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

ε祈祈猫儿з 提交于 2019-12-10 14:19:47

问题


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?


回答1:


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>


来源:https://stackoverflow.com/questions/1509238/check-if-a-observablecollection-and-if-so-display-an-alternative-xaml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!