C# 如何格式化TargetNullValue属性?
此属性在C# 如何格式化TargetNullValue属性?,c#,wpf,datatemplate,itemscontrol,targetnullvalue,C#,Wpf,Datatemplate,Itemscontrol,Targetnullvalue,此属性在ItemsControl上实现。我需要格式化或应用为斜体和灰色的字符串样式 <ItemsControl ItemsSource="{Binding Source={StaticResource SettingsViewSource}, TargetNullValue= 'No setting available'}" Background="Transparent"
ItemsControl
上实现。我需要格式化或应用为斜体和灰色的字符串样式
<ItemsControl ItemsSource="{Binding Source={StaticResource SettingsViewSource}, TargetNullValue= 'No setting available'}"
Background="Transparent"
HorizontalAlignment="Stretch"
Focusable="False">
不要使用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>
如果您想拥有更多的控制(如样式设置/格式设置)并基于datatrigger打开datatemplate,请定义并清空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>
//在此处定义数据模板。
注意:使用属性来确定ItemsControl是否包含项。添加一个对空样式作出反应的转换器?@Icepickle我认为
TargetNullValue
不是依赖属性
,因此我们无法为其应用转换器。