Hi Rolf,
Now I understand that your purpose is to use an SolidColorBrush to use the
MyColor property in Rectangle.Fill. Your code doesn't work because the
SolidColorBrush doesn't implement INotifyPropertyChanged and will not
automatically propagate the change when the MyColor property is changed.
The correct approach is to create a Converter for the Color ->
SolidColorBrush and assign this converter to the Binding:
[ValueConversion(typeof(Color), typeof(SolidColorBrush))]
public class ColorBrushConverter : IValueConverter
{
public object Convert(object value, Type targetType, object
parameter, System.Globalization.CultureInfo culture)
{
Color color = (Color)value;
return new SolidColorBrush(color);
}
public object ConvertBack(object value, Type targetType, object
parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}
Binding b = new Binding("MyColor");
b.Source = myWin;
b.Converter = new ColorBrushConverter();
BindingOperations.SetBinding(rect01, Rectangle.FillProperty, b);
This is described in following MSDN library page:
#Data Binding Overview
http://msdn2.microsoft.com/en-us/library/ms752347.aspx#data_conversion <quote>
However, what if instead of having a property of type string your binding
source object has a Color property of type Color? In that case, in order
for the binding to work you would need to first turn the Color property
value into something that the Background property accepts. You would need
to create a custom converter by implementing the IValueConverter interface,
</quote>
Hope this helps.
Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.