检查是否有ObservableCollection,如果是,则显示替代xaml!

cod*_*oop 6 data-binding wpf xaml observablecollection

我有一个ListView绑定到一个ObservableCollection.此外,我列出了所有项目ObservableCollection.现在,有没有一种好方法来检查它是否ObservableCollection为空,并显示另一个xaml?

jap*_*apf 8

您可以使用ListView的HasItems依赖项属性.使用触发器,当属性为false时,您可以更改ControlTemplate.这是例子:

<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>
Run Code Online (Sandbox Code Playgroud)