-1

I am not sure if this question has been asked before or not but i am currently in a situation where i bind my control's property to a DependencyProperty.However, the value returned by the Property is of type Double. When setting the binding, how do i subtract 20 or a given value from the property and then bind the control ? Do i need to implement IValueConverter for this ? I am still studying WPF so any help would be appreciated.

Dependency property

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);
    }
}

Binding to the property

 {
MainGrid = GetTemplateChild("MainGrid");
Binding MainGridWidthBinding = new Binding("PrValue")
{
    Source = this,
    Mode = BindingMode.TwoWay
};
MainGrid.SetBinding(Grid.WidthProperty, MainGridWidthBinding);
}
Software Dev
  • 5,368
  • 5
  • 22
  • 45

2 Answers2

0

To achieve your purpose, you need to write a class implement IValueConverter interface.

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. 
    }
}

Then set its instance to Binding.Converter property before call SetBinding method.

Binding MainGridWidthBinding = new Binding("PrValue")
{
    Source = this,
    Mode = BindingMode.TwoWay,
    Converter = new MyConverter(),
};
MainGrid.SetBinding(Grid.WidthProperty, MainGridWidthBinding);
Alex.Wei
  • 1,798
  • 6
  • 12
0

The best way is to implement an IValueConverter, here's an example.

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,
    }
}

And this is how you'd use it.

MainGrid = GetTemplateChild("MainGrid");
Binding MainGridWidthBinding = new Binding(nameof(PrValue)){
    Source = this,
    Mode = BindingMode.TwoWay,
    Converter = new DoubleModifierConverter 
    { 
        Modifier = 20.0, 
        Operator = Operator.Substraction 
    }
};
MainGrid.SetBinding(Grid.WidthProperty, MainGridWidthBinding);
Roger Leblanc
  • 1,543
  • 1
  • 10
  • 9