【问题标题】:How to format TargetNullValue property?如何格式化 TargetNullValue 属性?
【发布时间】:2015-09-04 11:27:16
【问题描述】:

此属性在ItemsControl 上实现。我需要将字符串格式化或应用样式为斜体和灰色。

<ItemsControl ItemsSource="{Binding Source={StaticResource SettingsViewSource}, TargetNullValue= 'No setting available'}" 
                              Background="Transparent" 
                              HorizontalAlignment="Stretch"
                              Focusable="False">

【问题讨论】:

  • 添加一个对空样式做出反应的转换器?
  • @Icepickle 我认为TargetNullValue 不是dependency property 因此我们不能为其应用转换器。

标签: c# wpf datatemplate itemscontrol targetnullvalue


【解决方案1】:

如果您想拥有更多控制(如样式/格式)并根据数据触发器切换数据模板,请定义和 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是否包含项目。

【讨论】:

    【解决方案2】:

    不使用 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>
    

    【讨论】:

      猜你喜欢
      • 2012-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多