WPF: Binding directly to a control
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 combinatio开发者_如何学编程ns 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.
The ElementName property is your friend
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.
精彩评论