Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF QuickConverter: Hide element on empty list

Tags:

c#

wpf

xaml

I have a StackPanel that holds a TextBox with a title and an ItemsControl with items. I'd like to hide the entire StackPanel if the list supplying the items is empty. Instead of writing a dedicated Converter for the binding, I wanted to give QuickConverter (https://quickconverter.codeplex.com/) a try. QuickConverter allows to use inline C# expressions in bindings.

So this is my mark-up:

<StackPanel Visibility="{qc:Binding '$P > 0 ? Visibility.Visible : Visibility.Collapsed', P={Binding Path=Value.Count}}"> <!-- this does not work. It's always shown, regardless of the element count -->
  <TextBlock Text="{qc:Binding '$P', P={Binding Path=Value.Count}}"></TextBlock> <!-- for debugging purposes only. It correctly shows the element count for the list -->
  <TextBlock Text="{qc:Binding '$P.Count', P={Binding Path=Value}}"></TextBlock> <!-- for debugging purposes only. It should do the same as the line above, but it does nothing -->

  ...

  <ItemsControl ItemsSource="{Binding Path=Value}">
    ...
  </ItemsControl>
</StackPanel>

The first textblock is displaying the expected result, all other QuickConverter expressions fail to work. There are no errors or exception, neither at design time nor at runtime.

Thank you for any ideas.

Chris.

like image 202
Christophe Avatar asked May 03 '16 08:05

Christophe


1 Answers

You may have a DataTrigger in a Style like this:

<StackPanel>
    <StackPanel.Style>
        <Style TargetType="StackPanel">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Value.Count}" Value="0">
                    <Setter Property="Visibility" Value="Collapsed"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </StackPanel.Style>
    ...
</StackPanel>
like image 59
Clemens Avatar answered Nov 15 '22 08:11

Clemens