通过xaml从itemcount更改TextBlock文本

时间:2016-07-23 13:41:59

标签: wpf

我有一个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进行?感谢。

2 个答案:

答案 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";
    }
相关问题