开发者

WPF: Binding directly to a control

开发者 https://www.devze.com 2022-12-10 15:04 出处:网络
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开发者_如

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 UserControlhas 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号