我有一个用户控件,我想在两种不同的上下文中使用它。用户控件需要将其 DataContext 设置为由父 View / View 模型创建的适当 View 模型的实例。
我希望有类似的东西:
<local:Child DataContext="{Binding ChildViewModel}"/>
其中 ChildViewModel 是页面绑定(bind)到的 ViewModel 的 inpc 样式属性。
这似乎行不通。是否可以使用 Binding 分配 DataContext?
最佳答案
将 ContentControl 的内容绑定(bind)到您的子 ViewModel 可能会更简单,如下所示:
<ContentControl Content="{Binding ChildViewModel}" />
..然后有一个DataTemplate来应用你的本地: subview ,像这样
<DataTemplate DataType="{x:Type local:ChildViewModel}">
<local:Child />
</DataTemplate>
关于silverlight - 是否可以将 UserControl 的 DataContext 绑定(bind)到属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4994595/