[英]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.