bitbonk

Is there a way to get notified about dependency property changes of children in the logical tree I want the a Grid to notify me (fire an event or something) in case one of its children that is a Textbox changed its TextProperty . Note that children of the grid change at runtime and it might well be possible that there are no children that are textboxes.


Re: Windows Presentation Foundation (WPF) get notified about childdren propertychanged

lee d

you can do something like this, but this is per control

DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(TextBox.TextProperty,typeof(TextBox));

dpd.AddValueChanged(txt1, new EventHandler(callback));






Re: Windows Presentation Foundation (WPF) get notified about childdren propertychanged

Drew Marsh

Is it that you're only interested in a specific property or you really want to know about every potential property change If the former, then you can walk the descendants of the logical tree of the Grid, determine if they're textboxes and hook up for property change notification using DependencyPropertyDescriptor::AddValueChanged. If the latter, while possible, I'd highly recommend against it and maybe with more detail I can provide a better solution.

Later,

Drew





Re: Windows Presentation Foundation (WPF) get notified about childdren propertychanged

bitbonk

No it should be as generic as possible. What I want is this: I created a custom panel that has an event called TextboxTextChanged. This event should be fired whenever both of the conditions are true:

  1. a Textbox changed its TextProperty
  2. the Textbox is a direct or indirect child of my custom panel





Re: Windows Presentation Foundation (WPF) get notified about childdren propertychanged

bitbonk

Drew Marsh wrote:

... If the former, then you can walk the descendants of the logical tree of the Grid, determine if they're textboxes and hook up for property change notification using DependencyPropertyDescriptor::AddValueChanged. If the latter, while possible, I'd highly recommend against it and maybe with more detail I can provide a better solution.

Later,

Drew

When do I walk the children Since the children can change I would need to walk the children everytime a direct or indirect child was added or removed, only to determine if the panel currently has children that are textboxes.






Re: Windows Presentation Foundation (WPF) get notified about childdren propertychanged

lee d

you can add a handler on the panel TextBox.TextChanged="txt_TextChanged" and that should catch all the textchanged events




Re: Windows Presentation Foundation (WPF) get notified about childdren propertychanged

Drew Marsh

Lee's answer is right on. All text boxes will route their TextChanged event up through the Grid. You need only listen to the event at the Grid level and WPF will take care of notifying you.

Later,

Drew





Re: Windows Presentation Foundation (WPF) get notified about childdren propertychanged

bitbonk

Thanks, lee. That solves my problem.

Allthough ...

... for the TextProperty that works fine. It works because there is a RoutedEvent for it. But what about a dependencyproperty where an approriate RoutedEvent does not exist. For example it seems there is no RoutedEvent for the HorizontalAlignmentProperty so I can not do <Grid TextBox.HorizontalAlignmentChanged="txt_HorizontalAlignmentChanged" />





Re: Windows Presentation Foundation (WPF) get notified about childdren propertychanged

lee d

yes, that method is not a solution for all the events, can you give some details about the scenario you are working on. so we will know the context




Re: Windows Presentation Foundation (WPF) get notified about childdren propertychanged

bitbonk

My concrete scenario is solved with the routedevent approach. So my last question was more a theoretical one. I could well imagine that some day I run into the same problem only with a property where the appropiate RoutedEvent does not exist. Let me try to make up a scenario:

Suppose I have a panel that should change its backgroundcolor based on backgroundcolors of its childelements. So whenever one of the panels direct or indirect children changes its Control.BackgroundPorperty (for example to make sure that the background of the panel is never the same as the background of its children) the panel needs to be notified upon each children's BackgroundPorperty change, because it has to recalculate its own background color now. Since there is no Control.BackgroundChanged I can not go with the RoutedEvent approach ...






Re: Windows Presentation Foundation (WPF) get notified about childdren propertychanged

lee d

you will need to have some logic to determine the color of the panel anyways and should have to bind the background to something so it can get a color,may be in that method, you can get background of the children by looping the children




Re: Windows Presentation Foundation (WPF) get notified about childdren propertychanged

bitbonk

Yes, but the problem is how can I get notified whenever a child changes its background Of course I could bind the background of my panel to each and every background of the children and execute some custom logic whenever a child's background changes. But that will only work if no children get added or removed at runtime. Because if that happens I have to setup the binding for newly added elements or remove the binding for removed elements.




Re: Windows Presentation Foundation (WPF) get notified about childdren propertychanged

lee d

you can just iterate over the children




Re: Windows Presentation Foundation (WPF) get notified about childdren propertychanged

bitbonk

lee d wrote:
you can just iterate over the children

The question is when to iterate. The iteration over the children (to get all children background colors) must be triggered by something, i.e. the change of a background property of one of the children. If we go witht the described databinding appoach, I still would have to set up a databinding for each newly added child manualy wich would again require me to write my own panel that can execute custom logic (i.e. the databinding) upon addition of children.