How to collapse databound TextBlock

Hello,

I'd like to set the "Visibility" property of a databound TextBlock based on whether there is any content or not. In the following example, there is an empty XML element<Department>, so the TextBlock in row 2 should be collapsed. What is the simplest way to do this? What is the simplest way to do this?

Thanks,

Adrian


<FixedPage
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="21cm" Height="29.7cm">
<FixedPage.Resources>
<XmlDataProvider x:Key="AddressData" XPath="CustomerAddress">
<x:XData>
<CustomerAddress xmlns="">
<Company>Company Name</Company>
<Department></Department>
<Name>Recipient Name</Name>
<Street>Musterstrasse 123</Street>
<City>9876 Hintertupfiken</City>
<Country>SWITZERLAND</Country>
</CustomerAddress>
</x:XData>
</XmlDataProvider>
</FixedPage.Resources>
<!-- CustomerAddress Grid-->
<Grid FixedPage.Left="2cm" FixedPage.Top="5cm" TextBlock.FontSize="12pt"
ShowGridLines="True"
DataContext="{Binding Mode=OneTime, Source={StaticResource AddressData}}">
<Grid.ColumnDefinitions>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="Billing Address:" LineHeight="48" FontWeight="Bold"/>
<TextBlock Grid.Row="1" Text="{Binding XPath=Company}"/>
<!-- since the "Department" element is empty, the textblock / grid row shuld be COLLAPSED -->
<TextBlock Grid.Row="2" Text="{Binding XPath=Department}"/>
<TextBlock Grid.Row="3" Text="{Binding XPath=Name}"/>
<TextBlock Grid.Row="4" Text="{Binding XPath=Street}"/>
<TextBlock Grid.Row="5" Text="{Binding XPath=City}"/>
<TextBlock Grid.Row="6" Text="{Binding XPath=Country}"/>
</Grid>
</FixedPage>

[3386 byte] By [AdrianBetschart] at [2007-12-24]
# 1

try setting up a style and use it like

<TextBlock Grid.Row="2" Text="{Binding XPath=Department}" Style="{StaticResource st1}" />

<Style x:Key="st1" TargetType="{x:Type TextBlock}">

<Style.Triggers>

<DataTrigger Binding="{Binding XPath= Department}" Value="">

<Setter Property="Visibility" Value="Collapsed"></Setter>

</DataTrigger>

</Style.Triggers>

</Style>

leed at 2007-10-7 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 2
Hey, Lee, your approach is right, but since the oringal poster places the textblock into the grid row, although you can collapse the textblock, but I don't think you can hide the grid row entirely using above code, the white space line is still over there.

Sheva

footballism at 2007-10-7 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 3
Sorry, I am completely wrong.

Sheva

footballism at 2007-10-7 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 4

As the height is set to Auto you dont see that row

leed at 2007-10-7 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 5

Thank you, Lee!

Follow-up question: Would it also be possible to define one style that I can use also for the other TextBlocks?

<TextBlock Grid.Row="2" Text="{Binding XPath=Department}" Style="{StaticResource st1}" />
<TextBlock Grid.Row="3" Text="{Binding XPath=Name}" Style="{StaticResource st1}" />
...

Best regards,

Adrian

AdrianBetschart at 2007-10-7 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 6
remove x:Key="st1" from the style. Then the style applies to all textblocks
lester-MSFT at 2007-10-7 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 7

If anyone is interested:

The following XAML now does what I intended. Maybe somebody cares to check if it's correct? Thanks for your help!


<FixedPage

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Width="21cm" Height="29.7cm">

<FixedPage.Resources>

<Style x:Key="AutoCollapse" TargetType="{x:Type TextBlock}">

<Style.Triggers>

<!--DataTrigger Binding="{Binding XPath=Department}" Value=""-->

<DataTrigger Binding="{Binding Path=Text, RelativeSource={RelativeSource Self}}" Value="">

<Setter Property="Visibility" Value="Collapsed" />

</DataTrigger>

</Style.Triggers>

</Style>

<XmlDataProvider x:Key="AddressData" XPath="CustomerAddress">

<x:XData>

<CustomerAddress xmlns="">

<Company>Company Name</Company>

<Department></Department>

<Name>Recipient Name</Name>

<Street>Musterstrasse 123</Street>

<City>9876 Hintertupfiken</City>

<Country>SWITZERLAND</Country>

</CustomerAddress>

</x:XData>

</XmlDataProvider>

</FixedPage.Resources>

<!-- CustomerAddress Grid-->

<Grid FixedPage.Left="2cm" FixedPage.Top="5cm" TextBlock.FontSize="12pt"

ShowGridLines="True"

DataContext="{Binding Source={StaticResource AddressData}}">

<Grid.ColumnDefinitions>

<ColumnDefinition />

</Grid.ColumnDefinitions>

<Grid.RowDefinitions>

<RowDefinition Height="Auto"/>

<RowDefinition Height="Auto"/>

<RowDefinition Height="Auto"/>

<RowDefinition Height="Auto"/>

<RowDefinition Height="Auto"/>

<RowDefinition Height="Auto"/>

<RowDefinition Height="Auto"/>

<RowDefinition Height="Auto"/>

</Grid.RowDefinitions>

<TextBlock Grid.Row="0" Text="Billing Address:" LineHeight="48" FontWeight="Bold"/>

<TextBlock Grid.Row="1" Text="{Binding XPath=Company}" Style="{StaticResource AutoCollapse}"/>

<TextBlock Grid.Row="2" Text="{Binding XPath=Department}" Style="{StaticResource AutoCollapse}"/>

<TextBlock Grid.Row="3" Text="{Binding XPath=Name}" Style="{StaticResource AutoCollapse}"/>

<TextBlock Grid.Row="4" Text="{Binding XPath=Street}" Style="{StaticResource AutoCollapse}"/>

<TextBlock Grid.Row="5" Text="{Binding XPath=City}" Style="{StaticResource AutoCollapse}"/>

<TextBlock Grid.Row="6" Text="{Binding XPath=Country}" Style="{StaticResource AutoCollapse}"/>

</Grid>

</FixedPage>

AdrianBetschart at 2007-10-7 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 8

you can change the style to

<Style TargetType="{x:Type TextBlock}">

...</Style>

and remove the attribute Style

Style="{StaticResource AutoCollapse}" from the TextBlock altogether( or move the style in to Grid.Resources so other textblocks wont get effected

leed at 2007-10-7 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...

Visual Studio Orcas

Site Classified