如果<run>为空WPF,则隐藏文本块

时间:2016-08-17 12:33:58

标签: c# wpf xaml

如果属性TextBlock为空,我想要隐藏Commission.Vehicle.Owner.Name。代码看起来像这样。当然这不起作用,TextBlock属性文本将始终为空..

<Style x:Key="CollapsableTextBlock" TargetType="TextBlock">
    <Style.Triggers>
        <Trigger Property="Text" Value="">
           <Setter Property="Visibility" Value="Collapsed"/>
        </Trigger>
    </Style.Triggers>
</Style>

<TextBlock Style="{DynamicResource CollapsableTextBlock}">
    <Run Text="Owner: "/>
    <Run Text="{Binding Commission.Vehicle.Owner.Name}"/>
</TextBlock>

任何有想法的人?谢谢!

1 个答案:

答案 0 :(得分:6)

DataTrigger应该可以工作:

<Style x:Key="CollapsableTextBlock" TargetType="TextBlock">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Commission.Vehicle.Owner.Name}" Value="">
            <Setter Property="Visibility" Value="Collapsed"/>
        </DataTrigger>
    </Style.Triggers>
</Style>