Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a ObservableCollection, and if so display an alternative xaml!

I have a ListView with a binding to a ObservableCollection. Further I am listing out all items in the ObservableCollection. Now, Is there a good way to check if the ObservableCollection is empty, and the display an alternative xaml?

like image 839
code-zoop Avatar asked Oct 02 '09 12:10

code-zoop


1 Answers

You can use the HasItems dependency property of the ListView. With a trigger, when the property is false, you can change the ControlTemplate. Here is as example:

<ListView ItemsSource="{Binding Items}">
  <ListView.Style>
    <Style TargetType="{x:Type ListView}">
      <Style.Triggers>
        <Trigger Property="HasItems" Value="False">
          <Setter Property="Template">
            <Setter.Value>
              <ControlTemplate TargetType="{x:Type ListView}">
                <Border SnapsToDevicePixels="true" 
                        Background="{TemplateBinding Background}" 
                        BorderBrush="{TemplateBinding BorderBrush}" 
                        BorderThickness="{TemplateBinding BorderThickness}">
                  <TextBlock Text="No items"
                             HorizontalAlignment="Center"
                             VerticalAlignment="Center"/>
                </Border>
              </ControlTemplate>
            </Setter.Value>
          </Setter>
        </Trigger>
      </Style.Triggers>
    </Style>
  </ListView.Style>
</ListView>
like image 88
japf Avatar answered Sep 30 '22 12:09

japf