Re: wpf - databinding problem

by "Walter Wang [MSFT]" on 11/1/2007 2:48:00 AM 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.

 

Re: wpf - databinding problem

by Rolf Welskes on 11/1/2007 10:10:00 PM Hello,
thank you,
but what you make is a work around of the problem.
This is not my problem.
The problem is: The Color of the Brush must be changed
and this change must be visible in the rectangle where the brush is the
background.
It is the chaining of the binding what is the problem, because this is only
a test for the chaining.
I bind rect-background to brush then I bind Color of the brush to
MyColor-Property.
this is the chain of binding.
Now the MyColor-Property changes the color,
now the brush has an other color and the rectangle must have the new color
of the brush.

The workaround is not usable in other scenarios.

Thank you and best regards
Rolf Welskes



""Walter Wang [MSFT]"" <wawang@online.microsoft.com> schrieb im Newsbeitrag
news:Be0oxGDHIHA.4200@TK2MSFTNGHUB02.phx.gbl...
> 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.
>


 

Re: wpf - databinding problem

by "Walter Wang [MSFT]" on 11/5/2007 6:02:00 AM Hi Rolf,

Thanks for your reply.

Your code was directly binding to the SolidColorBrush, however, it doesn't
implement INotifyPropertyChanged. As a result, it will not notify you when
its property is changed.

Actually you should create a public property to return the Window's
SolidColorBrush and you can bind to the window and use the property path:

        SolidColorBrush br;

        public SolidColorBrush MyBrush
        {
            get { return br; }
            set { br = value; }
        }


        public Window1()
        {
            InitializeComponent();

            br = new SolidColorBrush(Colors.SaddleBrown);

            MyColor = Colors.Red;

            Binding bdx01 = new Binding();
            bdx01.Source = this;
            bdx01.Path = new PropertyPath("MyBrush");
            BindingOperations.SetBinding(rect01, Rectangle.FillProperty,
bdx01);

            Binding bdx02 = new Binding();
            bdx02.Source = this;
            bdx02.Path = new PropertyPath("MyColor");
            BindingOperations.SetBinding(br,
SolidColorBrush.ColorProperty,bdx02);

            //txDummy.Background = br;
        }



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.

 

Re: wpf - databinding problem

by Rolf Welskes on 11/6/2007 2:22:00 PM Hello,
thank you for your answer.
BUT - I do not belive this.
Brush is a Freezable - and if Color changes as sub-property - Brush should
inform the target where it is bound.

But you have made another change and this shows where the proble is.

In the original code I sent to you
replace:

bdx02.ElementName = "myWin";
to
 bdx02.Source = this´;

now it works.

So, seems using Binding.ElementName in code is a problem,
I have tried it in xaml - no problem.

Binding.ElementName should give the same object
as setting Binding.Source - but seems it does not.
WHY?

Thank you for your help and best regards
Rolf Welskes


 

Re: wpf - databinding problem

by "Walter Wang [MSFT]" on 11/8/2007 1:42:00 AM Hi Rolf,

It turns out a known issue of WPF, we have an open bug for it. The root
cause here is that setting binding source via ElementName in code doesn't
set the SolidColorBrush's inheritance context.

#Nick on Silverlight and WPF : What's an inheritance context?
http://blogs.msdn.com/nickkramer/archive/2006/08/18/705116.aspx

If we assign the SolidColorBrush to the dummy TextBlock.Background, since
the TextBlock is also in the logical tree, the brush now gets correct
inheritance context.

We're sorry for the inconvenience caused. Please feel free to let me know
if there's anything else I can help.


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.

 

Re: wpf - databinding problem

by Rolf Welskes on 11/10/2007 1:50:00 AM Hello,
thank you,
so the problem in this sence is solved.
Thank you again and best regards
Rolf Welskes

""Walter Wang [MSFT]"" <wawang@online.microsoft.com> schrieb im Newsbeitrag
news:pmRfWiaIIHA.360@TK2MSFTNGHUB02.phx.gbl...
> Hi Rolf,
>
> It turns out a known issue of WPF, we have an open bug for it. The root
> cause here is that setting binding source via ElementName in code doesn't
> set the SolidColorBrush's inheritance context.
>
> #Nick on Silverlight and WPF : What's an inheritance context?
> http://blogs.msdn.com/nickkramer/archive/2006/08/18/705116.aspx
>
> If we assign the SolidColorBrush to the dummy TextBlock.Background, since
> the TextBlock is also in the logical tree, the brush now gets correct
> inheritance context.
>
> We're sorry for the inconvenience caused. Please feel free to let me know
> if there's anything else I can help.
>
>
> 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.
>