检查一个ObservableCollection,如果显示一个替代的xaml! [英] Check if a ObservableCollection, and if so display an alternative xaml!

查看:147
本文介绍了检查一个ObservableCollection,如果显示一个替代的xaml!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ListView ,绑定到 ObservableCollection 。此外,我列出了 ObservableCollection 中的所有项目。现在,有没有一个很好的方法来检查 ObservableCollection 是否为空,并显示一个替代的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?

推荐答案

可以使用ListView的HasItems依赖属性。使用触发器,当属性为false时,可以更改ControlTemplate。例如:

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>

这篇关于检查一个ObservableCollection,如果显示一个替代的xaml!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆