【问题标题】:Data Trigger where value is not Null值不为 Null 的数据触发器
【发布时间】:2019-06-11 14:53:15
【问题描述】:

我想设置一些值不为空的文本。
XAML:

<TextBlock >
   <TextBlock.Style>
      <Style TargetType="{x:Type TextBlock}">
         <Setter Property="Text" Value="solved"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Report}" Value="{x:Null}">
                   <Setter Property="Text" Value=""/>
                 </DataTrigger>
            </Style.Triggers>
      </Style>
   </TextBlock.Style>
</TextBlock>

注意:这里 ReportSTRING,它具有一些随机值,例如(例如 11,112,11a)

它在每一行都显示已解决,似乎数据触发器不起作用。

但它使用此随机值(例如 11,112,11a)使用此代码

 <TextBlock Text="{Binding Report}"/>

我希望 已解决 作为文本,而不是随机值(例如 11,112,11a),否则 空白 没有价值。 p>

【问题讨论】:

  • 您是否可以将另一个属性“兄弟”添加到报告中,比如说“ReportText”,到视图模型?如果您向我们展示一些报告属性所在的代码,这也会很有用。
  • 您的代码看起来不错。我已经在使用 {x:Null} 作为值时遇到了麻烦。有时只需设置 value = "" 然后返回 {x:Null} 即可解决问题。

标签: wpf xaml datatrigger


【解决方案1】:

你可以像这样使用转换器:

xaml:

xmlns:cnv="clr-namespace:Converters"
...
<TextBlock Text="{Binding Report, Converter={cnv:CustomNullToStringConverter NotNullValue=Solved,NullValue=''}}"/>

转换器:

class CustomNullToStringConverter : MarkupExtension, IValueConverter
{
    public string NullValue { get; set; }
    public string NotNullValue { get; set; }
    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is string && String.IsNullOrEmpty(value as string)) return NullValue;
        if (value == null) return NullValue;
        return NotNullValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

【讨论】:

    猜你喜欢
    • 2011-08-11
    • 1970-01-01
    • 2010-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-06
    • 1970-01-01
    相关资源
    最近更新 更多