询问者
如何绑定用户控件的DataContext?

问题
-
全部回复
-
Hi,
单单看你的代码,是可以绑定成功的;
建议你提供更多的代码,或者通过OneDrive上传一个小demo? 我会帮你测试。
Sincerely,
Bob
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com. -
Hi Neo Xiao,
我下载了你的Sample,问题不在你之前贴出的代码而在“Tab_Board.xaml”中,在这个xaml文件中,你对DataGrid的ItemSource进行了绑定:
ItemsSource="{Binding Boards, UpdateSourceTrigger=PropertyChanged}"
这段代码是有问题的,因为Boards这个属性并不存在Tab_Board控件中,而且在MainWindow.xaml中已经对绑Tab_Board的DataContext绑定,所以在Tab_Board.xaml中只需要这样修改就行:
ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=DataContext}"
另外,Boards的是ObservableCollection类型你可以不设置UpdateSourceTrigger=PropertyChanged,而设置BindsDirectlyToSource=True。
你也可以看看MSDN上关于绑定的介绍。
Best wishes,
Lee- 已建议为答案 Li Jun Jie 2017年12月27日 6:24