WPF 条件不等于时触发改变颜色

  1. 我需要一个 WPF下的Style,它可以在满足多个条件时设置多个属性.但是,我的一个条件是Not Equal To类型.以使条件变为不等于?
//需要一个IValueConverter和一些额外的标记:
<Style>
        <Style.Triggers>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <!--<Condition 1 here.../>-->
                    <!--<Condition 2 here.../>-->
                    <Condition>
                        <Condition.Binding>
                            <Binding Path="id" Converter="{StaticResource ValueToEqualsParameterConverter}">
                                <Binding.ConverterParameter> //没有定义全局Style
                                    <System:Int32>3</System:Int32> //没有定义全局Style
                                </Binding.ConverterParameter> //没有定义全局Style
                            </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>
                                
    //如果有定义全局Style,则注释上面三行,在全局Style中加入
     <local:ValueToEqualsParameterConverter x:Key = "ValueToEqualsParameterConverter"/>                           
                                
转换器: 
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; 
    }
}
posted @ 2020-12-30 15:11  DingGuo  阅读(812)  评论(0编辑  收藏  举报