0

我有一些标题的以下模板:

<telerik:RadExpander.Header>
    <StackPanel Orientation="Horizontal" DataContext="{Binding CurrentItem, ElementName=activityProductGrid}">
        <TextBlock Text="{Binding Strings[Product], Source={StaticResource LanguageResources}, StringFormat='{}{0}: '}" />
        <TextBlock Text="{Binding Product.Name}" FontWeight="Bold" />
        <TextBlock Text="{Binding Strings[GroupName], Source={StaticResource LanguageResources}, StringFormat=', {0}: '}" />
        <TextBlock Text="{Binding GroupName}" />
        <TextBlock Text="{Binding Strings[UnitPrice], Source={StaticResource LanguageResources}, StringFormat=', {0}: '}" />
        <TextBlock Text="{Binding UnitPrice}" />
    </StackPanel>
</telerik:RadExpander.Header>

如何对标头进行条件格式化,例如,如果 Product == null,那么标头不应显示任何内容?

编辑:这个有效。

<StackPanel Orientation="Horizontal" DataContext="{Binding CurrentItem, ElementName=activityProductGrid}">
    <i:Interaction.Triggers>
        <ie:DataTrigger Binding="{Binding}" Value="{x:Null}">
            <ie:ChangePropertyAction TargetObject="{RelativeSource={RelativeSource AncestorType=StackPanel}}" PropertyName="Visibility">
                <ie:ChangePropertyAction.Value>
                    <Visibility>Collapsed</Visibility>
                </ie:ChangePropertyAction.Value>
            </ie:ChangePropertyAction>
        </ie:DataTrigger>
    </i:Interaction.Triggers>

    <-- from this point the came code as above -->
</StackPanel>
4

1 回答 1

0

特别是对于 null,您可以为替换值指定绑定参数,或者在绑定错误的情况下显示一个:

<TextBlock 
  Text="{Binding Product.Name, TargetNullValue=(empty), FallbackValue=(error)}"/>

(取自WPF 绑定备忘单)

编辑:我注意到您不仅希望特定绑定具有不同的值,而且不希望显示整个控件。为此,您可以使用样式和触发器,将 a 绑定DataTrigger到您的Product属性,并设置Visibility它是否为空。

<telerik:RadExpander.Header>
    <StackPanel>
            <StackPanel.Style>
              <Style TargetType="StackPanel">
                <Style.Triggers>
                  <DataTrigger Binding="{Binding Product}" Value="{x:Null}">
                    <Setter Property="Visibility" Value="Collapsed"/>
                  </DataTrigger>
                </Style.Triggers>
              </Style>
            </StackPanel.Style>
        </StackPanel>
</telerik:RadExpander.Header>
于 2012-04-23T17:38:05.260 回答