c#绑定前更改属性值
我不确定以前是否有人问过这个问题,但我目前的情况是将控件的属性绑定到c#绑定前更改属性值,c#,wpf,C#,Wpf,我不确定以前是否有人问过这个问题,但我目前的情况是将控件的属性绑定到DependencyProperty。但是,属性返回的值是Double类型。设置绑定时,如何从属性中减去20或给定值,然后绑定控件?我是否需要为此实施IValueConverter?我仍在学习WPF,因此任何帮助都将不胜感激 依赖项属性 public static readonly DependencyProperty ProgressbarValueDependency = DependencyProperty.Registe
DependencyProperty
。但是,属性返回的值是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);
}
绑定到属性
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);
}
为了达到您的目的,您需要编写一个类实现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
属性 <代码>设置绑定
方法
Binding MainGridWidthBinding = new Binding("PrValue")
{
Source = this,
Mode = BindingMode.TwoWay,
Converter = new MyConverter(),
};
MainGrid.SetBinding(Grid.WidthProperty, MainGridWidthBinding);
最好的方法是实现IValueConverter,下面是一个示例
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,
}
}
这就是你使用它的方式
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);
是的,你应该实施IVlaueConverter@user1672994,我以前从未使用过它…您能否发布有关此问题的答案,以便我能够解决此问题并让我们了解IValueConverter的工作原理?@user1672994,如果我想使用IValueConverter,是否需要保留上述代码?您可以参考和