如何为基于ItemsControl的控件(如ListView或DataGrid)定义空的DataTemplate [英] How-to define Empty DataTemplate for the ItemsControl based controls like ListView or DataGrid

查看:49
本文介绍了如何为基于ItemsControl的控件(如ListView或DataGrid)定义空的DataTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像ListView这样的ASP.NET控件允许通过设置

ASP.NET controls like ListView allows providing a custom template by setting the ListView.EmptyDataTemplate property, this template will be rendered in case of empty data source.

对于 ItemsControl 的控件,例如 ListView DataGrid ?因此,当 ItemsSource 为空.

How to do the same in WPF (XAML only preferrable) for ItemsControl based controls like ListView and DataGrid? So I want to show my custom DataTemplate in case when ItemsSource is empty.

推荐答案

您可以使用基于DataTrigger的Template属性设置

You can use set the Template property based on a DataTrigger

例如

在资源中:

<ControlTemplate x:Key="EmptyListBoxTemplate">
     <TextBlock Text="Items count == 0" />
</ControlTemplate>

控件本身:

<ListBox ItemsSource="{Binding SomeCollection}">
    <ListBox.Style>
        <Style TargetType="{x:Type ListBox}">
            <Style.Triggers>
                <DataTrigger Value="{x:Null}" Binding="{Binding DataContext.SomeCollection, RelativeSource={RelativeSource Self}}">
                    <Setter Property="Template" Value="{StaticResource EmptyListBoxTemplate}" />
                </DataTrigger>
                <DataTrigger Value="0" Binding="{Binding DataContext.SomeCollection.Count, RelativeSource={RelativeSource Self}}">
                    <Setter Property="Template" Value="{StaticResource EmptyListBoxTemplate}" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ListBox.Style>
</ListBox>

可能会有一种更简单的绑定方法,但是我现在没有编译器来弄清楚它是什么:)

There might be an simplier way of doing the binding, but I don't have a compiler on me right now to figure out what it would be :)

这篇关于如何为基于ItemsControl的控件(如ListView或DataGrid)定义空的DataTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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