views:

42

answers:

1

Hi there,

how can i inform all subControls in WPF that they shall for example expand? Not every SubControl shall be informed.. only those who can e.g. accept an other element for drop.

Perhaps you know another posibilty to let the items change their color when they do accept specific dropContent.

Thanks, el

+1  A: 

Option 1

If your subcontrols are implemented in your own code, a good way is to:

  1. Create a custom RoutedEvent
  2. Send the routed event from the parent whenever you want to inform the subcontrols of something
  3. Handle the routed event in the subcontrols and respond with the desried behavior (for example expanding if they are drop targets)

You can add parameters to your routed event's EventArgs to inform subcontrols of the specifics of the change you are informing them about.

Option 2

If you want to affect controls that you don't create and have no control over, you can recursively descend the visual tree using VisualTreeHelper.GetChildrenCount() and VisualTreeHelper.GetChild() and make whatever changes you want to the controls you find. This is more powerful than the RoutedEvent method but in many cases does not provide as good an abstraction, leading to code that is harder to maintain in the long run.

Option 3

Use an inherited attached property. Setting the property on the parent will cause the property to propagate down to each descendant, calling your PropertyChangedCallback each time. In your PropertyChangedCallback you can make whatever changes you want to the child controls.

This can be better than descending the visual tree recursively because you will also get PropertyChangedCallbacks if new subcontrols are added to your tree. This means that new children that are added later (eg through data binding) will also get the updated features (eg they will also expand if they are drop targets). Another advantage is that inherited properties propagates down the logical tree as well as the visual. A disadvantage is they can be slower than the VisualTreeHelper technique, and are slightly more code.

Ray Burns
thank you for your awnser. I will try it in the next weeks and give you feedback about this.
elCapitano