自动调整按钮(文本)内容的大小有问题。
这是我的扣子的样式。除了注意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 )。
我找到了一个solution来计算所需的文本大小。所以我可以计算按钮的最大FontSize。因此,我需要我的一个按钮的实际大小(所有按钮都有相同的大小)。
我真的不知道如何得到大小和解决我的问题。如果有人有个主意或者一个完全不同的方法,那就太好了。
发布于 2016-04-24 19:55:12
您可以使用按钮的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;
}https://stackoverflow.com/questions/36826327
复制相似问题