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

查看:31
本文介绍了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 中,有多个 TextBox 和其他控件,它们将它们的值绑定到 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-only 方式更好.

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天全站免登陆