如何在同一个用户控件中绑定到不同的datacontexts [英] How do I bind to different datacontexts within the same usercontrol

查看:71
本文介绍了如何在同一个用户控件中绑定到不同的datacontexts的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ItemsControl,其Itemssource绑定到一组customWidgets,如下所示:

I have a ItemsControl whose Itemssource is bound to a collection of customWidgets as follows:

<ItemsControl ItemsSource ="{Binding MyCustomWidgets}">
 <ItemsControl.ItemTemplate>
  <DataTemplate>
   <CustomWidget Name="{Binding Name}" 
     SomeOtherDP="{Binding SomePropertyOfaCustomWidget}" 
     EditButtonCommand="{Binding   Path=MyButtonCommand,DataContext=MyViewModel}
   </CustomWidget>
  </DataTemplate>
 </ItemsControl.ItemTemplate>
</ItemsControl>

The ViewModel:
public class MyViewModel
	Inherits ViewModelBase
	public Property MyCustomWidgets as ObservableCollection(Of CustomWidget)
	   ... omitted for brevity    
        End Property
	
	Public Property MyButtonCommand as ICommand
	   ... omitted for brevity
	End Property
End Class



Widget usercontrol上有一个按钮它内部绑定到名为EditButtonCommand的ICommand类型的Custom Dependency属性



我希望除了EditButton之外的Widget的所有属性都绑定到CustomWidget的实例。 />
但我想要EditButton(即Widget的自定义依赖项属性绑定到ItemsControl的datacontext设置为的相同MyViewModel实例的MyButtonCommand属性,而不是CustomWidget。

似乎我应该能够指向Button使用某种相对祖先verbage绑定到ItemsControl的datacontext(其datacontext设置为ViewModel),但我无法弄清楚如何执行此操作。

有谁知道怎么做这个?


The Widget usercontrol has a button on it that is internally bound to a Custom Dependency property of type ICommand named EditButtonCommand

I want all properties of the Widget except the EditButton to bind to an instance of CustomWidget.
But I want the EditButton (i.e. the custom dependency property of the Widget) to bind to the MyButtonCommand Property of the same MyViewModel instance that the ItemsControl's datacontext is set to, not the CustomWidget.
It seems that I should be able to point the Button Binding to the datacontext of the ItemsControl (whose datacontext is set to the ViewModel) using some sort of relative ancestor verbage, but I can't figure out how to do this.
Does anyone know how to do this?

推荐答案

您好,您可以为按钮绑定命令属性如下 -

Hello, You can bind the Command property for Button in ItemsControl as follow -
<Button x:name="button1" Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext.MyButtonCommand}" Commandparameter="{Binding ElementName=button1}" />





这样你就可以绑定 EditButtonCommand 与上面的Button Command属性相同。



So that you can bind EditButtonCommand same as above Button Command property.


我经过多次试验和错误后想出来了。

I figured it out after much trial and error.
EditButtonCommand ="{Binding DataContext.EnableCommand, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ItemsControl}}}"



这是解决问题的正确xaml代码。我还是不太明白这一切。我得到了ItemsControl类型的FindAncestor,但看似多余的RelativeSource = {RelativeSource ...是什么?


This is the correct xaml code to solve the problem. I still don't quite understand all of this. I get the FindAncestor of type ItemsControl, but what is the seemingly redundant "RelativeSource={RelativeSource ..." about?


这篇关于如何在同一个用户控件中绑定到不同的datacontexts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆