tags:

views:

233

answers:

2

I need to bind a property of a UserControl directly to another control, rather than any specific property on that control. What's the best way to achieve this? I've tried various combinations of the Binding properties to no avail.

For some context, the UserControl has a Next property that specifies which control is next in the navigation hierarchy; it's similar to TabIndex but for context sensitive navigation.

<c:MyControl x:Name="First" Next="{Binding ???}" />
<c:MyControl x:Name="Second" />

From reading the docs, I assumed I should've been able to do: {Binding Source=Second, BindsDirectlyToSource=True}, but that didn't work.

+3  A: 

The ElementName property is your friend

ArildF
I tried `ElementName` before without any success, but after some further investigation it ends up it was working fine, but how I was debugging it was wrong. My overridden event was being called against `Second`, without me realising, so `Next` was always null. Once I realised this and got the event raising against `First`, the property was set.
James Gregory
Nice and succinct.
Ray Burns
A: 

ArildF's answer {Binding ElementName=Second} is the best direct answer to your question, but have you considered using the WPF's built in navigation functionality?

<c:MyControl x:Name="First" KeyboardNavigation.TabIndex="1" />
<c:MyControl x:Name="Second" KeyboardNavigation.TabIndex="2" />

Also check out:

  • KeyboardNavigationMode enum

  • KeyboardNavigation.DirectionalNavigation / TabNavigation / ControlNavigation

  • KeyboardNavigation.IsTabStop

  • Using <Grid> instead of <DockPanel> to keep controls in natural order

It may be that the functionality you desire is already covered by WPF.

Ray Burns
Thanks for the suggestion Ray, and certainly for the example given it would be better to use what you've said; however, my example is an oversimplification of the design.
James Gregory