WPF触发器不为空

PaN*_*1Me 5 wpf triggers

如何在属性不为null时在WPF中触发操作?当为null时,这是一个有效的解决方案:

<Style.Triggers>
    <DataTrigger Binding="{Binding}" Value="{x:Null}">

      <Setter Property="Background" Value="Yellow" />

    </DataTrigger>
</Style.Triggers>
Run Code Online (Sandbox Code Playgroud)

我知道你不能"扭转"这种情况并做你需要的事情,但想知道

Tho*_*que 10

不幸的是,你做不到.但实际上没有必要:您只需要在样式设置器中指定值不为空的背景,而不是在触发器中:

<Style.Setters>
    <!-- Background when value is not null -->
    <Setter Property="Background" Value="Blue" />
</Style.Setters>
<Style.Triggers>
    <DataTrigger Binding="{Binding}" Value="{x:Null}">

      <Setter Property="Background" Value="Yellow" />

    </DataTrigger>
</Style.Triggers>
Run Code Online (Sandbox Code Playgroud)


yos*_*rel 7

您可以使用Expression Blend附带的Microsoft.Expression.Interactions.dll中的DataTrigger类.

代码示例:

<i:Interaction.Triggers>
    <ie:DataTrigger Binding="{Binding YourProperty}" Value="{x:Null}" Comparison="NotEqual">
       <ie:ChangePropertyAction PropertyName="YourTargetPropertyName" Value="{Binding YourValue}"/>
    </ie:DataTrigger>
</i:Interaction.Triggers>
Run Code Online (Sandbox Code Playgroud)

使用这种方法,你可以触发打击GreaterThanLessThan太.要使用此代码,您应该引用两个dll:

System.Windows.Interactivity.dll
Microsoft.Expression.Interactions.dll

并添加相应的命名空间:

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"  
xmlns:ie="http://schemas.microsoft.com/expression/2010/interactions"
Run Code Online (Sandbox Code Playgroud)