如果您想拥有更多控制(如样式/格式设置)并根据数据触发器切换数据模板,请定义和 EmptyDataTemplate。
例如
<ItemsControl ItemsSource="{Binding Source={StaticResource SettingsViewSource}}"
Background="Transparent"
HorizontalAlignment="Stretch"
Focusable="False">
<ItemsControl.ItemTemplate>
<DataTemplate>
//Define your data template here.
</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>
注意:使用HasItems属性来确定 ItemsControl 是否包含项目。