[WPF] data binding and SetValue()

L

Lloyd Dupont

I have a color chooser class
class ColorPicker : Control
{
public byte R { get; set; }
public byte G { get; set; }
public byte B { get; set; }
public Color Color { get; set; }
}

All are dependency properties.
All these dependency properties are obviously related, when Red change,
Color change and vice versa.

Now I wonder how to update them as a whole.

If I do it directly mysefl as in:
When R change I call SetValue(ColorProperty, newValue)
That feel wrong.

Because if the user call, let's say:
ClearValue(ColorProperty) I end up calling SetValue(RProperty, ..), while I
should in fact clear it...

does it makes sense?
How should I synchronize values?
 
N

Nicholas Paldino [.NET/C# MVP]

Lloyd,

Are you attaching an event handler to the DependencyProperty instance
for when those values change? You should be able to set event handlers for
each of them (or rather, one for the R, G, B, values, and one for the Color
value) and then modify the values in the event handler. When you call
SetValue from the event handler, it should prevent the setting of the values
from triggering the respective change events, and subsequently, an endless
loop.
 
L

Lloyd Dupont

Hi Nicholas,

My worry are not from endless loop which I know how to prevent, but more
from other problem which I'm not sure it's even relevant...
(the fact that I'm calling SetValue() sometime when ClearValue() would be
more appropriate)

--
Regards,
Lloyd Dupont
NovaMind Software
Mind Mapping at its best
www.nova-mind.com
Nicholas Paldino said:
Lloyd,

Are you attaching an event handler to the DependencyProperty instance
for when those values change? You should be able to set event handlers
for each of them (or rather, one for the R, G, B, values, and one for the
Color value) and then modify the values in the event handler. When you
call SetValue from the event handler, it should prevent the setting of the
values from triggering the respective change events, and subsequently, an
endless loop.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Lloyd Dupont said:
I have a color chooser class
class ColorPicker : Control
{
public byte R { get; set; }
public byte G { get; set; }
public byte B { get; set; }
public Color Color { get; set; }
}

All are dependency properties.
All these dependency properties are obviously related, when Red change,
Color change and vice versa.

Now I wonder how to update them as a whole.

If I do it directly mysefl as in:
When R change I call SetValue(ColorProperty, newValue)
That feel wrong.

Because if the user call, let's say:
ClearValue(ColorProperty) I end up calling SetValue(RProperty, ..), while
I should in fact clear it...

does it makes sense?
How should I synchronize values?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top