.net - 如何为基于 ItemsControl 的控件(如 ListView 或 DataGrid)定义 Empty DataTemplate

标签 .net wpf xaml datatemplate itemscontrol

像 ListView 这样的 ASP.NET 控件允许通过设置 ListView.EmptyDataTemplate 来提供自定义模板。属性,此模板将在空数据源的情况下呈现。

如何在 WPF 中为 ItemsControl 做同样的事情(只推荐 XAML)基于控件,如 ListViewDataGrid ?所以我想在 ItemsSource 的情况下显示我的自定义 DataTemplate是空的。

最佳答案

有一个 100% xaml 解决方案,它利用了“HasItems”依赖属性。

<ItemsControl>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Description}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.Style>
        <Style TargetType="ItemsControl">
            <Style.Triggers>
                <Trigger Property="HasItems" Value="false">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <TextBlock Text="This Control is empty"/>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ItemsControl.Style>
</ItemsControl>

关于.net - 如何为基于 ItemsControl 的控件(如 ListView 或 DataGrid)定义 Empty DataTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8154213/

相关文章:

c# - 在第二个 lambda 中使用一个 lambda 的逻辑

c# - 执行 RenderTransform(缩放和平移)后如何获得 UIElement 的新位置和大小?

wpf - ContentControl 崩溃中的绑定(bind)

c# - 以编程方式将访问控制列表 (ACL) 权限分配给 'this folder, subfolders and files'

c# - .NET 核心测试 - 使用 FakeItEasy 模拟 IHttpContextAccessor

c# - HttpWebRequest 对于分块数据很慢

wpf - 如何在 mvvm 应用程序中的每个文本框焦点事件上调用事件触发器

c# - WPF TabControl Position Tabs 在右上角

c# - 如何重新设置 DataGridTextColumn 的 TextBox 的样式?

c# - 如何在 WPF 上使用 ICommand 的 CanExecute 方法