ContextMenu命令绑定未随数据源更新 [英] ContextMenu Command Binding not updating with the DataSource

查看:94
本文介绍了ContextMenu命令绑定未随数据源更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的WPF应用程序中,有一个列表可供选择.然后,所选项目将显示在ContentControl中,以进行进一步的交互.根据所选项目的类型(可以有多个),在ContentControl中使用适当的DataTemplate.到目前为止,对于数据处理业务应用程序来说应该没有什么异常.

In my WPF application, there is a list to select an item from. The selected item will then be displayed in a ContentControl for further interaction. Depending on the type of the selected item (there can be several), an appropriate DataTemplate is used in the ContentControl. So far, this should be nothing unusual for data processing business applications.

在每个DataTemplate中,都有多个TextBoxes和其他控件,这些控件将其值绑定到ViewModel类的特定属性.从列表中选择另一个项目时,所有这些都将按预期进行更新.这些按钮还会在ViewModel的正确实例上执行命令.

In each DataTemplate, there are multiple TextBoxes and other controls that bind their value to a specific property of the ViewModel class. When selecting another item from the list, all of these are updated as expected. The buttons also execute their command on the correct instance of the ViewModel.

有一个上下文菜单项也可以执行命令,但这仅适用于第一个选定的项.当从列表中选择相同类型的另一个元素时,重新使用已经加载的模板视图,上下文菜单中的命令将始终在第一个选择的项目上执行.因此,绑定不会更新为ViewModel的正确实例.

There is one context menu item that also executes a command, but this will only work for the first selected item. When another element of the same type is selected from the list, re-using the already loaded template view, the command from the context menu will always be executed on the first-selected item. So the binding is not updated to the correct instance of the ViewModel.

使菜单项使用正确的ViewModel实例的唯一方法是选择其他类型的项,以便将模板更改为另一个视图.只有这样,上下文菜单才能正确更新.

The only way to make the menu item use the correct ViewModel instance is to select an item of a different type so that the template is changed to another view. Only then the context menu is updated correctly.

为什么菜单项命令不会像视图中的任何其他绑定一样被更新?加载后将其提取一次,但在该视图的生命周期中从未更新过.

Why won't the menu item command be updated just like any other binding in the view? It is fetched once upon loading but never updated for the view's lifetime.

推荐答案

不是Command绑定不会更新,而是DataContext已过期.一旦您知道正确的搜索词,这就是一个广为人知的问题...

It's not the Command binding that doesn't update, it's the DataContext that's out of date. And this is a widely known issue, once you know the right search terms...

以下是带有更多链接的说明:

Here's an explanation with further links:

http://www.codeproject. com/Articles/162784/WPF-ContextMenu-Strikes-Again-DataContext-Not-Upda

这是该文章的相关部分:

Here's the relevant part of that article:

解决方法是将菜单的数据上下文显式绑定到父级的数据上下文,如下所示:

The workaround is to explicitly bind menu's data context to parent's datacontext as follows:

<ContextMenu DataContext="{Binding PlacementTarget.DataContext,
    RelativeSource={RelativeSource Self}}">

这个神奇的咒语告诉WPF在菜单的数据上下文与其放置目标"(即父对象)数据上下文之间创建永久绑定,即使在父对象的数据上下文更改后,该绑定也继续起作用.仅当您期望父级的数据上下文在父级生命周期中发生变化时,才需要使用此咒语.

This magical spell tells WPF to create a permanent binding between the menu's data context and its "placement target" (i.e. parent) data context, which continues to work even after parent's data context is changed. You need this spell only if you expect parent's data context to change during the life of the parent.

我之前找到的另一个解决方案是在Opened事件中将上下文菜单的DataContext手动设置为窗口的DataContext.这需要在代码隐藏文件中添加其他C#代码,并且可能需要适应不同的情况.因此,我认为上面的仅XAML方式会更好.

Another solution I've previously found was to manually set the context menu's DataContext to the window's DataContext in the Opened event. This requires additional C# code in the code-behind file and may need to be adapted to different scenarios. So I think the XAML-only way above is better.

这篇关于ContextMenu命令绑定未随数据源更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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