如果值等于或大于 WPF 触发器将起作用 [英] WPF Trigger that would work if the value is equal or greater

查看:45
本文介绍了如果值等于或大于 WPF 触发器将起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 WPF 编写了一个应用程序,它有一个按钮和滑块.我想为按钮创建一个触发器,当滑块值大于时,它会将按钮的IsEnable"属性设置为 false.现在我有:

I wrote an application in WPF that has a button and slider. I would like to create a trigger for the button, which would set the button's 'IsEnable' property to false when the slider value is greater than another value. Right now I have:

<Style x:Key="zoomOutButton" TargetType="Button" BasedOn="{StaticResource ResourceKey=buttonStyle}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding CurrentAltitude}" Value="24000">
                <Setter Property="IsEnabled" Value="False" />
            </DataTrigger>
        </Style.Triggers>
    </Style>

但我想设置 isEnable 不是当 CurrentAltitude 的值等于 24000 时,而是当它等于或大于 24000 时.有什么想法吗?

But I would like to set isEnable not when the value of CurrentAltitude equal 24000, but when it is equal or greater than 24000. Any ideas?

推荐答案

您可以使用转换器来实现:

You can achieve this using a converter:

public class IsEqualOrGreaterThanConverter : IValueConverter {
    public static readonly IValueConverter Instance = new IsEqualOrGreaterThanConverter();

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
        int intValue = (int) value;
        int compareToValue = (int) parameter;

        return intValue >= compareToValue;
    }

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

那么您的触发器将如下所示:

Then your trigger will look like this:

<Style x:Key="zoomOutButton" TargetType="Button" BasedOn="{StaticResource ResourceKey=buttonStyle}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding CurrentAltitude, Converter={x:Static my:IsEqualOrGreaterThanConverter.Instance}, ConverterParameter=24000}" Value="True">
            <Setter Property="IsEnabled" Value="False" />
        </DataTrigger>
    </Style.Triggers>
</Style>

这篇关于如果值等于或大于 WPF 触发器将起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆