c#绑定前更改属性值

时间:2018-06-07 18:30:49

标签: c# wpf

我不确定此问题是否曾被提出过,但我目前处于将我的控件的属性绑定到DependencyProperty的情况。但是,Property返回的值是类型为Double。设置绑定时,如何从属性中减去20或给定值,然后绑定控件?我需要为此实施IValueConverter吗?我还在学习WPF,所以任何帮助都会受到赞赏。

依赖属性

public static readonly DependencyProperty ProgressbarValueDependency = DependencyProperty.Register("PrValue", typeof(double),     typeof(LinearProgressBar));

public double PrValue
{
    get
    {
        return System.Convert.ToDouble(GetValue(ProgressbarValueDependency));
    }
    set
    {
        SetValue(ProgressbarValueDependency, value);
    }
}

绑定到属性

 {
MainGrid = GetTemplateChild("MainGrid");
Binding MainGridWidthBinding = new Binding("PrValue")
{
    Source = this,
    Mode = BindingMode.TwoWay
};
MainGrid.SetBinding(Grid.WidthProperty, MainGridWidthBinding);
}

2 个答案:

答案 0 :(得分:0)

为了达到您的目的,您需要编写一个类实现IValueConverter接口。

public class MyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, 
        object parameter, string language)
    {
        // Contain your source to target convertion logic. 
    }

    public object ConvertBack(object value, Type targetType, 
        object parameter, string language)
    {
        // Contain your target to source convertion logic.
        // Only needed if using TwoWay or OneWayToSource Binding Mode. 
    }
}

然后在调用Binding.Converter方法之前将其实例设置为SetBinding属性。

Binding MainGridWidthBinding = new Binding("PrValue")
{
    Source = this,
    Mode = BindingMode.TwoWay,
    Converter = new MyConverter(),
};
MainGrid.SetBinding(Grid.WidthProperty, MainGridWidthBinding);

答案 1 :(得分:0)

最好的方法是实现IValueConverter,这是一个例子。

SharedViewModel

这就是你如何使用它。

using System;
using System.Windows.Data;

namespace WpfApp1
{
    public class DoubleModifierConverter : IValueConverter
    {
        public double Modifier { get; set; }
        public Operator Operator { get; set; } = Operator.Addition;

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (!(value is double doubleValue))
                throw new InvalidCastException();

            switch (Operator)
            {
                case Operator.Addition:
                    return doubleValue + Modifier;
                case Operator.Substraction:
                    return doubleValue - Modifier;
                case Operator.Multiplication:
                    return doubleValue * Modifier;
                case Operator.Division:
                    return doubleValue / Modifier;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (!(value is double doubleValue))
                throw new InvalidCastException();

            switch (Operator)
            {
                case Operator.Addition:
                    return doubleValue - Modifier;
                case Operator.Substraction:
                    return doubleValue + Modifier;
                case Operator.Multiplication:
                    return doubleValue / Modifier;
                case Operator.Division:
                    return doubleValue * Modifier;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
    }

    public enum Operator
    {
        Addition = 0,
        Substraction = 1,
        Multiplication = 2,
        Division = 3,
    }
}