当条件不等于时触发 [英] Trigger when condition is not equal to
本文介绍了当条件不等于时触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要 WPF 下的 Style
来在满足多个条件时设置多个属性.但是,我的条件之一是 Not Equal To
类型.我应该如何更改下面的 Style
以便条件变为 Not Equal To
?甚至可以在没有 IValueConverter
的情况下实现吗?
I need a Style
under WPF that sets several properties when multiple conditions are fullfilled. However, one of my conditions is of type Not Equal To
. How should I change the below Style
so that the condition would become Not Equal To
? Can it be even achieved without IValueConverter
?
<Style>
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<!--<Condition 1 here.../>-->
<!--<Condition 2 here.../>-->
<Condition Binding="{Binding Path=id}" Value="3"/>
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="Red"/>
<Setter Property="Foreground" Value="Black"/>
</MultiDataTrigger>
</Style.Triggers>
</Style>
我需要以下内容,但这当然不起作用,因为触发器仅支持 Equal
运算符.
I would need the below but this of course don't work since Triggers only support Equal
operator.
<Style>
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<!--<Condition 1 here.../>-->
<!--<Condition 2 here.../>-->
<Condition Binding="{Binding Path=id}" Value<>"3"/>
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="Red"/>
<Setter Property="Foreground" Value="Black"/>
</MultiDataTrigger>
</Style.Triggers>
</Style>
推荐答案
你需要一个 IValueConverter
和一些额外的标记:
You need an IValueConverter
and some extra markup for this:
<Style>
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<!--<Condition 1 here.../>-->
<!--<Condition 2 here.../>-->
<Condition>
<Condition.Binding>
<Binding Path="id" Converter="{StaticResource ValueToEqualsParameterConverter}">
<Binding.ConverterParameter>
<System:Int32>3</System:Int32>
</Binding.ConverterParameter>
</Binding>
</Condition.Binding>
<Condition.Value>
<System:Boolean>False</System:Boolean>
</Condition.Value>
</Condition>
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="Red" />
<Setter Property="Foreground" Value="Black" />
</MultiDataTrigger>
</Style.Triggers>
</Style>
和转换器:
public class ValueToEqualsParameterConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
return value == parameter;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
return null;
}
}
这篇关于当条件不等于时触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文