WPF中绑定转换IValueConverter

版权声明:[email protected] https://blog.csdn.net/cmdszh/article/details/8313507

目的:把源对象中的属性(typeof(int))绑定到目标对象(UI)的backGround属性(typeof(brush))

关键点:绑定的类型不匹配的时候,指定IValueConverter的实例做类型转换


[ValueConversion(typeof(int),typeof(SolidColorBrush))]

public class Converter1:IValueConverter
{


#region IValueConverter 成员

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int i = (int)value;
Color c = Colors.Blue;
c.R = (byte)(i % 255);
SolidColorBrush brush = new SolidColorBrush(c);
return brush;
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}

#endregion
}

猜你喜欢

转载自blog.csdn.net/cmdszh/article/details/8313507