WPF 绑定(bind)到另一个元素的 ViewModel

标签 wpf binding viewmodel

只是一个例子。 CustomControl 有一个带有名为“Test”的属性的 ViewModel

如何将文本框绑定(bind)到该特定属性?
您可以访问 sibling ViewModel 吗?

<TextBox Text="{Binding ElementName=myControl, Path=ViewModel.Test}"></TextBox>
<Controls:CustomControl x:Name="myControl" />

最佳答案

Siblings ViewModel 将在其 DataContext 中尝试

<TextBox Text="{Binding ElementName=myControl, Path=DataContext.Test}"></TextBox>
<Controls:CustomControl x:Name="myControl" />

关于WPF 绑定(bind)到另一个元素的 ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5626138/

相关文章:

c# - 来自 TreeView 控件的绑定(bind)错误

javascript - Knockoutjs 计算传递参数

c# - WPF - ComboBox SelectedItem 未绑定(bind)

silverlight - 如何将父 View 中的参数设置为在用户控件中声明为静态资源的 View 模型

wpf - 适用于电脑和手机的应用程序

wpf - 如何使用正确的 Windows 系统颜色?

c# - 按字母顺序添加到 ObservableCollection

c# - 使用依赖属性绑定(bind)图像源

wpf - 应该如何将事件从一个 ViewModel 传播到 MVVM 中的另一个 ViewModel?

android - 我从不进入我的 ViewModel 中的 LiveDataScope