我有一个Expander
,其中包含一系列元素,这就是结构:
<Expander IsExpanded="True" Background="#4F4F4F">
<Expander.Header>
<StackPanel Orientation="Horizontal" Height="22">
<TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="White" FontSize="22" VerticalAlignment="Bottom" />
<TextBlock Text="{Binding ItemCount}" FontSize="22" Foreground="Orange" FontWeight="Bold" FontStyle="Italic" Margin="10,0,0,0" VerticalAlignment="Bottom" />
<TextBlock Text=" match" FontSize="22" Foreground="White" FontStyle="Italic" VerticalAlignment="Bottom" />
</StackPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
我想要实现的是,当第二个TextBlock的ItemCount为> 1
时,最后一个TextBlock match
的文本将在matches
中自动更改,这是否可以通过xaml进行?感谢。
答案 0 :(得分:1)
您可以在Style
中设置TextBlock文本<StackPanel Orientation="Horizontal" Height="22">
<TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="White" FontSize="22" VerticalAlignment="Bottom" />
<TextBlock Text="{Binding ItemCount}" FontSize="22" Foreground="Orange" FontWeight="Bold" FontStyle="Italic" Margin="10,0,0,0" VerticalAlignment="Bottom" />
<TextBlock FontSize="22" Foreground="White" FontStyle="Italic" VerticalAlignment="Bottom" >
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Text" Value=" matches" />
<Style.Triggers>
<DataTrigger Binding="{Binding ItemCount}" Value="1">
<Setter Property="Text" Value=" match" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</StackPanel>
答案 1 :(得分:0)
在纯Xaml中执行此操作没那么有意义。
您可以为此编写转换器
<TextBlock Text="{Binding ItemCount, Converter={StaticResource ItemCountConverter}}" />
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (int)value > 1 ? "matches" : "match";
}