Reputation:
I have to display a big list of properties/values. My issue is that there aren't values for all these properties, so I would like NOT to display these in that case.
It will be easier to understand my problem with some code:
<StackPanel DataContext=...>
<TextBlock>Info1:<TextBlock Text={Binding Path=Info1} /></TextBlock>
<TextBlock>Info2:<TextBlock Text={Binding Path=Info2} /></TextBlock>
<TextBlock>Info3:<TextBlock Text={Binding Path=Info3} /></TextBlock>
</StackPanel>
Basically, depending on the content of the child TextBlock, I would like to collapse the parent TextBlock. My idea was to use a style that applies to all the TextBlock and to check for content, and if there isn't any, to collapse the parent... unfortunately, I can't manage to access to the parent from the DataTrigger. Probably you'll have a solution more neat than that :)
Thanks a lot for your help!
Upvotes: 1
Views: 1496
Reputation: 35954
If you are inside a template or style, you can use triggers to set the visibility of the outer textblock.
For example, in case of DataTemplate:
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=Info1}" Value="">
<Setter Property="Visibility" TargetName="pnlInfo1" Value="Hidden" />
</DataTrigger>
<!-- and so on ... -->
</DataTemplate.Triggers>
Adjust the trigger according to your needs. For example you can hide it when it is null
or use a converter as gcores suggested to do more fancy checking.
Upvotes: 1
Reputation: 12656
You could bind the Visibility of the outer textblock to the text of the inner textblock using a simple converter:
<TextBlock Name="outer1"
Visibility="{Binding ElementName=inner1, Path=Text, Converter={StaticResource MyConverter}}">
Info1:<TextBlock Name="inner1" Text="{Binding Path=Info1}" />
</TextBlock>
or bind the Visibility of the outer textblock directly to Info1:
<TextBlock Visibility="{Binding Path=Info1, Converter={StaticResource MyConverter}}">
Info1:<TextBlock Text="{Binding Path=Info1}" />
</TextBlock>
Upvotes: 2