获取 WPF 资源中 DataContext 更改的通知
我在 WPF 绑定方面遇到了一个恼人的问题。基本上,我在 UserControl 的资源中声明了一个 FrameworkElement,但是当父 UserControl 的 DataContext 更改时,该项目似乎没有收到通知。
基本上,在我的 UserControl 中,我在 ItemsControl 的 ItemTemplate 中有一个弹出窗口。在该 Popup 中,我需要绑定到父视图的 ViewModel 中的某些内容,因此我想出了一个我认为很聪明的技巧。从 CollectionViewSource 得到提示,我想我只需将父级的视图模型绑定到资源,然后使用该资源从 DataTemplate 获取 ViewModel,如下所示: 这样
<UserControl.Resources>
<cont:ViewModelSource Source="{Binding}" x:Key="ParentViewModel"/>
</UserControl.Resources>
我就可以像这样使用它:
CommandParameter="{Binding ViewModel.OpenEntity, Source={StaticResource ParentViewModel}}"
以后 似乎可以工作除了当 UserControl 的 DataContext 重置时 ViewModelSource 上的 DataContext 不会重置。现在,我正在以黑客方式进行这项工作:在 UserControl 的 DataContextChanged 事件的代码隐藏中设置资源的 DataContext。
我在 Reflector 中查看了 CollectionViewSource 是如何做到这一点的,但它似乎没有做任何特别的事情。
有人知道为什么会发生这种情况或者我该如何解决它?
I'm having an annoying problem with WPF binding. Basically, I declare a FrameworkElement in my UserControl's resources, but that item doesn't seem to get notified when the DataContext of the parent UserControl changes.
Basically, in my UserControl I have a Popup in the ItemTemplate for an ItemsControl. In that Popup, I needed to bind to something in the parent view's ViewModel, so I came up with what I thought was a clever trick. Taking a cue from CollectionViewSource, I figured I'd just bind my parent's view model to a resource, and then use that resource to get to the ViewModel from the DataTemplate, like so:
<UserControl.Resources>
<cont:ViewModelSource Source="{Binding}" x:Key="ParentViewModel"/>
</UserControl.Resources>
So that later I can use it like:
CommandParameter="{Binding ViewModel.OpenEntity, Source={StaticResource ParentViewModel}}"
This all would seem to work except that the DataContext on the ViewModelSource doesn't get reset when the DataContext of the UserControl gets reset. Right now, I'm making this work hackily: setting the resource's DataContext in code-behind on the UserControl's DataContextChanged event.
I took a look in Reflector to see how CollectionViewSource does this, but it doesn't seem to be doing anything special.
Anyone know why this is happening or how I can fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有同样的问题,我找到了解决方案。
首先,我尝试将 ViewModel 设置为根元素的 DataContext。错误的。
然后我尝试将 ViewModel 设置为资源并将根元素的绑定源设置为资源。错误的。
最后,我创建了一个 IValueConverter 将模型(即控件的 DataContext)转换为 ViewModel。然后我将根元素的 DataContext 与转换器绑定。
我认为当 DataContext 破坏元素中的绑定时,当根元素上的数据上下文发生更改时,它会停止在破坏的绑定上。
I had the same problem and I found a solution.
First i tryed to set my ViewModel as DataContext of my root element. wrong.
Then i tryed to set my ViewModel as Resource and set binding source of my root element to the Resource. wrong.
Finally I created a IValueConverter to convert a Model (witch is DataContext of the control) to a ViewModel. Then I Bind DataContext of my root element with the converter.
I think that when the DataContext breaks the binding in an element, when the datacontext changes on a root element, it stops on the broken binding.
也许您必须创建一个实现 INotifyPropertyChanged 接口的中间对象。
Maybe you have to create a middle object that implement
INotifyPropertyChanged
interface.