Question

Here's the problem I often encounter when I develop complex controls in WPF.

I want to bind some properties up to multiple levels of parent.

I'm currently using this with RelativeSource and TemplatedParent :

Value="{Binding PropertyToBind, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"

But with this, you can just bind to the direct parent, and I have 4 levels... So I made 4 binding to finally bind to the good value.

I cannot remove my 4 levels of object, they are complex controls...

For example, my first level is my Joystick control. In my joystick, I have a Slider control (2nd level) to move up and down. In my Slider, I have a Button (3rd level). In my button, I have a Text (4th level) that I want to bind to a propety in my level 1 (Joystick).

What I do is I bind my text to a property in the button, then I bind the property of the button to the one in the Slider, etc.. to the Joystick. Sometimes, I need to create custom controls (with added properties where I can bind).

This is not my scenario, it's just an example (yes in this we can probably remove some levels but not in mine) I don't want an answer with: remove this or this object...

Is there a way to bind directly to multiple levels? Each level is a ControlTemplate in a Style.

I'm using WPF 4, .NET 4.0, Blend 4, Visual Studio 2010

Was it helpful?

Solution

Have you tried the other permutations of RelativeSource in your binding? Specifically, you can use RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type SomeType}} to find a specific control above you, which looks like it might work well for your scenario.

OTHER TIPS

You should not do this, it breaks the encapsulation and intrOduces unnecessary dependencies. What you describes is the way to go:

What I do is I bind my text to a property in the button, then I bind the property of the button to the one in the Slider, etc..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top