如何使用绑定和转换器声明可见性

时间:2014-12-03 15:15:11

标签: wpf

到目前为止,我还没有提取重复的可见性绑定到一个样式(现在在Stackpanel.Resources中)。有可能吗?

<StackPanel Orientation="Vertical">
    <TextBlock Text="{Binding FullName}" Visibility="{Binding FullName, Converter={StaticResource StringToVisibilityConverter}}"/>
    <TextBlock Text="{Binding Email}" Visibility="{Binding Email, Converter={StaticResource StringToVisibilityConverter}}"/>
    <TextBlock Text="{Binding OfficePhone}" Visibility="{Binding OfficePhone, Converter={StaticResource StringToVisibilityConverter}}"/>
    <TextBlock Text="{Binding MobilePhone}" Visibility="{Binding MobilePhone, Converter={StaticResource StringToVisibilityConverter}}"/>
</StackPanel>

我尝试过这样的事情:

<StackPanel Orientation="Vertical">
    <StackPanel.Resources>
         <Style TargetType="TextBlock">
             <Setter Property="Visibility" Value={Binding ???, Converter={StaticResource StringToVisibilityConverter}} />
         </Style>
    </StackPanel.Resources>
    <TextBlock Text="{Binding FullName}"/>
    <TextBlock Text="{Binding Email}"/>
    <TextBlock Text="{Binding OfficePhone}"/>
    <TextBlock Text="{Binding MobilePhone}"/>
</StackPanel>

1 个答案:

答案 0 :(得分:2)

如果你总是关闭文本的绑定,那么你可以这样做

     <Style TargetType="TextBlock">
         <Setter Property="Visibility" Value="{Binding Text, 
                         Converter={StaticResource StringToVisibilityConverter},
                         RelativeSource={RelativeSource Self}}" />
     </Style>

这会将可见性值绑定到转换后的文本值。