[英]WPF ItemsControl stretch FontSize of ButtonContent
I have a problem autosizing the (Text)Content of my buttons. 我在自动设置按钮的(Text)Content时遇到问题。
This is the style of my Buttons. 这是我的按钮的样式。 Nothing special but notice the TextWrapping property!
没什么特别的,但请注意TextWrapping属性!
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<TextBlock Text="{TemplateBinding Content}" TextWrapping="Wrap"/>
</Setter.Value>
</Setter>
</Style>
I defined a ItemTemplate based on my Buttons: 我根据我的按钮定义了一个ItemTemplate:
<DataTemplate x:Key="MyItemTemplate">
<Button Content="{Binding Content}" Command="{Binding Command}" FontWeight="Bold" FontSize="{Binding FontSize}"/>
</DataTemplate>
I am showing a list of items in with a ItemsControl with 3 columns: 我正在显示带有3列的ItemsControl的项目列表:
<ItemsControl ItemsSource="{Binding MyItems}" ItemTemplate="{StaticResource MyItemTemplate}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="3"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
Until this point, this is nothing special. 到目前为止,还没有什么特别的。 But now, I want the FontSize of all my Buttons stretched to maximum possible, the item with the largest content allows (all items should have the same FontSize in the end).
但是现在,我希望所有按钮的FontSize尽可能扩展到最大,内容最大的项目允许(所有项目最后都应具有相同的FontSize)。
I found a solution to calculate the needed size of a text. 我找到了一种解决方案来计算所需的文本大小。 So I could calculate the maximum FontSize of a Button.
所以我可以计算一个按钮的最大FontSize。 But therefore I need the actual size of one of my Buttons (all have the same size).
但是因此我需要一个按钮的实际大小(所有按钮都具有相同的大小)。
I have not really an idea how to get the size and solve my problem. 我真的不知道如何获取尺寸并解决我的问题。 Would be great if anybody has an idea - or a totally different approach.
如果有人有想法-或完全不同的方法,那将很棒。
You can use ActualWidth of your Button as CommandParameter to get the width like this : 您可以将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>
your Command should then be like this : 您的命令应如下所示:
public ICommand Command => new DelegateCommand<object>(ExecuteCommand);
private void ExecuteCommand(object obj)
{
double width = (double)obj;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.