版权声明:[email protected] https://blog.csdn.net/cmdszh/article/details/8313507
目的:把源对象中的属性(typeof(int))绑定到目标对象(UI)的backGround属性(typeof(brush))
关键点:绑定的类型不匹配的时候,指定IValueConverter的实例做类型转换
[ValueConversion(typeof(int),typeof(SolidColorBrush))]
{
#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
}