开发者

WPF update context menu items before menu display

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}"
                      ItemContainerStyleSelecto开发者_JS百科r="{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


Personally, I'm a big fan of using OnPropertyChanged() from IPropertyNotifyChanged. This way you can create a command that gets executed on right clicking that calls OnPropertyChanged(). 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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜