WPF 在菜单显示之前更新上下文菜单项
我正在将 WPF 与 Caliburn 和 MVVM 模式结合使用,并尝试设置动态上下文菜单。
目前,我们有一个上下文菜单,其中的项目绑定到 ViewModel 上的 BindableCollection。
<UserControl.ContextMenu>
<ContextMenu ItemsSource="{Binding AvailableActions}"
actions:Action.TargetWithoutContext="{Binding}"
ItemContainerStyleSelector="{StaticResource NamedActionStyleSelector}"/>
</UserControl.ContextMenu>
public BindableCollection<NamedAction> AvailableActions { get; set; }
在程序运行期间,此 BindableCollection 在某些情况下会通过以下方法进行更新(再次在视图模型上):
private void UpdateAvailableActions()
当前系统在大多数情况下运行良好,但有一些边缘情况表明我们最好动态生成右键单击事件后的列表。
所以我的问题是,任何人都可以帮助我在用户右键单击时调用更新方法(或编写将被调用的替代方法)的最佳方法。任何有关如何执行此操作的帮助将不胜感激。
谢谢
I'm using WPF with Caliburn and the MVVM pattern and I'm trying to set up a dynamic context menu.
Currently we have a context menu with it's Items bound to a BindableCollection on the ViewModel.
<UserControl.ContextMenu>
<ContextMenu ItemsSource="{Binding AvailableActions}"
actions:Action.TargetWithoutContext="{Binding}"
ItemContainerStyleSelector="{StaticResource NamedActionStyleSelector}"/>
</UserControl.ContextMenu>
public BindableCollection<NamedAction> AvailableActions { get; set; }
This BindableCollection is updated in certain circumstances during the running of the program by the method (Again on the view model):
private void UpdateAvailableActions()
The current system works well for most situations but there are a few edge cases that suggest that we would be better served to dynamically generate the list after a right click event.
So my question is, can anyone help me with the best way to call the update method (or write an alternative method which would be called) when the user has right clicked. Any help with how to do this would be greatly appreciated.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就我个人而言,我非常喜欢使用
IPropertyNotifyChanged
。通过这种方式,您可以创建一个在右键单击时调用OnPropertyChanged()
执行的命令。由于您使用的是 MVVM,因此您必须自己做一些研究,了解如何将事件实现为命令,可以在 此处或这里。或者,您可以通过 CommandParameter 绑定发挥创意并在命令中使用它。希望有帮助!
Personally, I'm a big fan of using
OnPropertyChanged()
fromIPropertyNotifyChanged
. This way you can create a command that gets executed on right clicking that callsOnPropertyChanged()
. You will have to do a little research yourself about how you want to implement events as commands since you're using MVVM, a starting point can be found here or here. Alternatively, you can get creative with the CommandParameter binding and use that in your command.Hope that helps!