我有Style
定义的“英雄/垂直”按钮(里面有图片):
<ControlTemplate TargetType="{x:Type controls:ImageButton}">
<Grid MinHeight="{TemplateBinding MinHeight}" Background="{TemplateBinding Background}" Width="Auto" SnapsToDevicePixels="True">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<!-- Image -->
<Viewbox x:Name="ViewBoxInternal" Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Center" IsEnabled="{TemplateBinding IsEnabled}"
Stretch="Uniform" StretchDirection="Both" Effect="{x:Null}"
Width="{TemplateBinding MaxSize}" Height="{TemplateBinding MaxSize}"
MaxHeight="{TemplateBinding MaxSize}" MaxWidth="{TemplateBinding MaxSize}">
<ContentPresenter ContentSource="{TemplateBinding Content}" Width="Auto" Height="Auto"
HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Viewbox>
<!-- Text -->
<TextBlock x:Name="TextBlockInternal" Grid.Row="1"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="3,2,3,3"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Text="{TemplateBinding Text}"
TextWrapping="Wrap" Effect="{TemplateBinding Effect}" TextAlignment="Center"/>
</Grid>
</ControlTemplate>
用法:
<controls:ImageButton Text="Webcam Recording" Content="{StaticResource Vector.Camera.New}"
Margin="0" Height="70" MaxSize="30">
MaxSize
控制控件内图像的最大大小。
示例图片:
预期图片:
我的所有垂直按钮都有MaxWidth
60,这对English
没问题,问题从其他语言开始,当60px太小时,按钮应该横向变大,但仍然使用所有的高度。
那么,如何在扩展宽度之前使TextBlock
使用所有可用高度?
TextWrapping仅在没有可用宽度时有效,例如当我将宽度设置为60时。
答案 0 :(得分:1)
有趣的问题。在不知道最宽字的宽度的情况下,您将不知道如何定义TextBox的宽度以获得所需的自动包装。要全力以赴,您可以使用一些语言API并使用它的wordbreakers。我认为你的代码中有一个孤立的案例,也许你可以自己做,让TextBox自动调整大小,居中,并达到你想要的结果。
编写一个转换器以强制单词之间的中断。我把它们放在一起来展示这种技术......你当然希望适应你的解决方案。
<强>代码强>
public partial class MainWindow : Window
{
public string LongText
{
get { return (string)GetValue(LongTextProperty); }
set { SetValue(LongTextProperty, value); }
}
// Using a DependencyProperty as the backing store for LongText. This enables animation, styling, binding, etc...
public static readonly DependencyProperty LongTextProperty =
DependencyProperty.Register("LongText", typeof(string), typeof(MainWindow), new PropertyMetadata(string.Empty));
public MainWindow()
{
InitializeComponent();
DataContext = this;
Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
LongText = "Some Really Long Text";
}
}
public class WordBreakConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var text = (string)value;
text = text.Replace(" ", Environment.NewLine);
return text;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
<强> XAML 强>
<Window.Resources>
<local:WordBreakConverter x:Key="WordBreakConverter"/>
</Window.Resources>
<Grid x:Name="LayoutRoot">
<StackPanel>
<TextBlock Text="WPF" FontSize="36" Margin="20" Foreground="Orange" HorizontalAlignment="Center"/>
<TextBlock Text="{Binding LongText, Converter={StaticResource WordBreakConverter}}" TextAlignment="Center"/>
</StackPanel>
</Grid>
<强>结果强>