此属性在ItemsControl
上实施。我需要为字符串格式化或应用样式为斜体和灰色。
<ItemsControl ItemsSource="{Binding Source={StaticResource SettingsViewSource}, TargetNullValue= 'No setting available'}"
Background="Transparent"
HorizontalAlignment="Stretch"
Focusable="False">
答案 0 :(得分:1)
不要使用TargetNullValue,只需使用带有DataTrigger测试的样式为null:
<Style.Triggers>
<DataTrigger Binding="{Binding Source={StaticResource SettingsViewSource}}" Value="{x:Null}">
<Setter Property="FontStyle" Value="Italic" />
<Setter Property="Background" Value="Gray" />
</DataTrigger>
</Style.Triggers>
答案 1 :(得分:1)
定义和EmptyDataTemplate如果你想拥有更多控件,比如样式/格式化,并根据datatrigger切换datatemplate。
例如
<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是否包含项目。