Tôi đã cố gắng đạt được điều này bằng cách chỉnh sửa mẫu mặc định cho Button
, chủ yếu là thêm gói TextBlock
thay vì mặc định ContentPresenter
và tính Width
trong trình chuyển đổi. Cách tiếp cận này cần khá nhiều dữ liệu trong trình chuyển đổi, mặc dù có nhiều cách dễ dàng hơn (và tốt hơn) để làm điều này nhưng dường như nó vẫn hoạt động. Nó cần một tham chiếu đến PresentationFramework.Aero vì ButtonChrome
trong mẫu mặc định
Chỉ cần sử dụng nó như mẫu Ảnh chụp màn hình
<Button Style="{StaticResource WrappingButton}"
MinWidth="100"
Content="This button has some long text">
</Button>
này với 3 WrappingButtons
trong một StackPanel
WrappingButton Style
<Style x:Key="WrappingButton" TargetType="{x:Type Button}"
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero">
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="Width" Value="Auto"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<ControlTemplate.Resources>
<local:WrappingButtonWidthConverter x:Key="WrappingButtonWidthConverter"/>
</ControlTemplate.Resources>
<Microsoft_Windows_Themes:ButtonChrome x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}" RenderDefaulted="{TemplateBinding IsDefaulted}" SnapsToDevicePixels="true">
<TextBlock VerticalAlignment="Center"
FontSize="{TemplateBinding FontSize}"
FontFamily="{TemplateBinding FontFamily}"
FontStyle="{TemplateBinding FontStyle}"
FontWeight="{TemplateBinding FontWeight}"
FontStretch="{TemplateBinding FontStretch}"
LineStackingStrategy="BlockLineHeight"
TextWrapping="Wrap"
TextTrimming="WordEllipsis"
Text="{Binding RelativeSource={RelativeSource TemplatedParent},
Path=Content}">
<TextBlock.Width>
<MultiBinding Converter="{StaticResource WrappingButtonWidthConverter}">
<Binding RelativeSource="{RelativeSource Self}" Path="Text"/>
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FontFamily"/>
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FontStyle"/>
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FontWeight"/>
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FontStretch"/>
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FontSize"/>
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="MinWidth"/>
</MultiBinding>
</TextBlock.Width>
</TextBlock>
</Microsoft_Windows_Themes:ButtonChrome>
<ControlTemplate.Triggers>
<Trigger Property="IsKeyboardFocused" Value="true">
<Setter Property="RenderDefaulted" TargetName="Chrome" Value="true"/>
</Trigger>
<Trigger Property="ToggleButton.IsChecked" Value="true">
<Setter Property="RenderPressed" TargetName="Chrome" Value="true"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="#ADADAD"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
WrappingButtonWidthConverter
public class WrappingButtonWidthConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string text = values[0].ToString();
FontFamily fontFamily = values[1] as FontFamily;
FontStyle fontStyle = (FontStyle)values[2];
FontWeight fontWeight = (FontWeight)values[3];
FontStretch fontStretch = (FontStretch)values[4];
double fontSize = (double)values[5];
double minWidth = (double)values[6];
string[] words = text.Split(new char[] {' '});
double widthSum = 0.0;
List<double> wordWidths = GetWordWidths(words, fontFamily, fontStyle, fontWeight, fontStretch, fontSize, out widthSum);
double width = 0.0;
for (int i = 0; width < (widthSum/2.0) && i < wordWidths.Count; i++)
{
width += wordWidths[i];
}
return minWidth > 0.0 ? Math.Max(minWidth, width) : width;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
private List<double> GetWordWidths(string[] words,
FontFamily fontFamily,
FontStyle fontStyle,
FontWeight fontWeight,
FontStretch fontStretch,
double fontSize,
out double widthSum)
{
List<double> wordWidths = new List<double>();
widthSum = 0.0;
foreach (string word in words)
{
Typeface myTypeface = new Typeface(fontFamily, fontStyle, fontWeight, fontStretch);
FormattedText ft = new FormattedText(word + " ",
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
myTypeface,
fontSize,
Brushes.Black);
wordWidths.Add(ft.WidthIncludingTrailingWhitespace);
widthSum += ft.WidthIncludingTrailingWhitespace;
}
return wordWidths;
}
}
Sau đó, bạn có thể thiết lập MaxTextWidth của nó với chiều rộng nút, sau đó kiểm tra ft.Height một lần nữa. Nếu nó cao hơn 2x Chiều cao ban đầu, bạn có nhiều hơn 2 dòng và muốn đặt MaxHeight thành khoảng 2x Chiều cao ban đầu. –
Tôi sẽ chụp ảnh đó và cho bạn biết cách hoạt động. Cảm ơn ý tưởng! –
Tôi đã thực hiện gần như điều này chính xác với nó, vì vậy nếu nó không làm việc cho bạn cho tôi biết. Tôi chỉ cần đăng trong ý kiến như SO ngày hôm nay là tung ra lỗi khi tôi thực hiện bất cứ điều gì nhưng bài viết ngắn. –