[英]WPF ItemsControl stretch FontSize of ButtonContent
我在自动设置按钮的(Text)Content时遇到问题。
这是我的按钮的样式。 没什么特别的,但请注意TextWrapping属性!
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<TextBlock Text="{TemplateBinding Content}" TextWrapping="Wrap"/>
</Setter.Value>
</Setter>
</Style>
我根据我的按钮定义了一个ItemTemplate:
<DataTemplate x:Key="MyItemTemplate">
<Button Content="{Binding Content}" Command="{Binding Command}" FontWeight="Bold" FontSize="{Binding FontSize}"/>
</DataTemplate>
我正在显示带有3列的ItemsControl的项目列表:
<ItemsControl ItemsSource="{Binding MyItems}" ItemTemplate="{StaticResource MyItemTemplate}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="3"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
到目前为止,还没有什么特别的。 但是现在,我希望所有按钮的FontSize尽可能扩展到最大,内容最大的项目允许(所有项目最后都应具有相同的FontSize)。
我找到了一种解决方案来计算所需的文本大小。 所以我可以计算一个按钮的最大FontSize。 但是因此我需要一个按钮的实际大小(所有按钮都具有相同的大小)。
我真的不知道如何获取尺寸并解决我的问题。 如果有人有想法-或完全不同的方法,那将很棒。
您可以将Button的ActualWidth用作CommandParameter来获取宽度,如下所示:
<UserControl ....
.................>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding Command}" CommandParameter="{Binding ActualWidth,ElementName=button}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<DataTemplate x:Key="MyItemTemplate">
<Button Content="{Binding Content}" x:Name="button"
Command="{Binding Command}"
FontWeight="Bold" FontSize="{Binding FontSize}"/>
</DataTemplate>
您的命令应如下所示:
public ICommand Command => new DelegateCommand<object>(ExecuteCommand);
private void ExecuteCommand(object obj)
{
double width = (double)obj;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.