像 ListView 这样的 ASP.NET 控件允许通过设置 ListView.EmptyDataTemplate 来提供自定义模板。属性,此模板将在空数据源的情况下呈现。
如何在 WPF 中为 ItemsControl 做同样的事情(只推荐 XAML)基于控件,如 ListView和 DataGrid ?所以我想在 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/