AgainMe AgainMe - 3 months ago 15
C# Question

Remove row if textblock binded value is null

I just ask a similar question but I noticed for my bad, I need to hide the entire row and not only the textblock. Let me explain what happen. So I've a ListBox with this structure:

<ListBox VerticalAlignment="Stretch"
ItemsSource="{Binding EventInfo}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="20"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="Event:" FontWeight="Bold" Grid.Column="0" Grid.Row="0"/>
<TextBlock Text="{Binding Name}" FontWeight="Bold" Grid.Column="1" Grid.Row="0"/>
<TextBlock Text="Foo:" FontWeight="Bold" Grid.Column="0" Grid.Row="1"/>
<TextBlock Text="{Binding Foo}" FontWeight="Bold" Grid.Column="1" Grid.Row="1"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>


so what I need to do is hide all the rows that contains the
textblock
/s with a null or empty value, actually I managed this in pure xaml like this:

<ListBox.Resources>
<Style TargetType="TextBlock">
<Style.Triggers>
<Trigger Property="Text" Value="">
<Setter Property="Visibility" Value="Collapsed" />
</Trigger>
<Trigger Property="Text" Value="{x:Null}">
<Setter Property="Visibility" Value="Collapsed" />
</Trigger>
</Style.Triggers>
</Style>
</ListBox.Resources>


but this is not a good solution. Infact, how you can see I've each
textblock
organized by row and column, so I need to hide the row that contain the textblock with the null value.

With my solution, I only hide the textblock with null value, that is useless 'cause the value is already empty or null.

There is a chance to manage it through xaml? I've no idea how can I do this in xaml, 'cause if I hide a row in the middle for example, I'll get a blank space with the row that contains textblocks with not null value. I don't know if the situation is clear.

If something is unclear, ask and I'll try to explain better. Thanks.

Answer

You could set the Grid Row Height accordingly

Replace

<Grid.RowDefinitions>
    <RowDefinition/>
    <RowDefinition/>
</Grid.RowDefinitions>

with

<Grid.RowDefinitions>
    <RowDefinition>
        <RowDefinition.Style>
            <Style TargetType="RowDefinition">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Name}" Value="">
                        <Setter  Property="Height" Value="0" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Name}" Value="{x:Null}">
                        <Setter  Property="Height" Value="0" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </RowDefinition.Style>
    </RowDefinition>
    <RowDefinition>
        <RowDefinition.Style>
            <Style TargetType="RowDefinition">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Foo}" Value="">
                        <Setter  Property="Height" Value="0" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Foo}" Value="{x:Null}">
                        <Setter  Property="Height" Value="0" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </RowDefinition.Style>
    </RowDefinition>
</Grid.RowDefinitions>