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

时间:2009-10-02 12:30:36

标签: wpf xaml data-binding observablecollection

我有一个与ListView绑定的ObservableCollection。此外,我列出了ObservableCollection中的所有项目。现在,有没有一种方法可以检查ObservableCollection是否为空,并显示另一个xaml?

1 个答案:

答案 0 :(得分: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>