EagleBeak
EagleBeak

Reputation: 7419

How to declare visibility with binding and converter in style

I have so far failed to extract the repetitive visibility binding to a style (in Stackpanel.Resources for now). Is it possible?

<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>

I've tried something like this:

<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>

Upvotes: 0

Views: 76

Answers (1)

ndonohoe
ndonohoe

Reputation: 10230

If you are always going off the binding of text then you could do this

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

This will bind the value of visibility to the value of text after conversion.

Upvotes: 2

Related Questions