开发者

WPF: Change the text of menuitem on context menu

开发者 https://www.devze.com 2022-12-17 00:08 出处:网络
I have ContextMenu in XAML with the 开发者_JAVA技巧following MenuItem: <MenuItemCommand=\"local:MyClass.MyCommand\" \"/>

I have ContextMenu in XAML with the 开发者_JAVA技巧following MenuItem:

<MenuItem  Command="local:MyClass.MyCommand" "/>

I'd like to change the Header of MenuItem by somehow changing the Text of RoutedCommand. And I'd like to do that in CanExecuteRoutedEventHanlder. If I just change the RoutedCommand Text, MenuItems don't get updated, I assume that is because the menu is already created when CanExecuteRoutedEventHanlder is run.

Basically I need a method to force update from Command to MenuItem.


Make your own command:

 public class MyCommandClass : ICommand, INotifyPropertyChanged
{
    public string Text { get; set; }

    public MyCommandClass(string text)
    {
        this.Text = text;
    }

    public bool CanExecute(object parameter)
    {
        this.Text = "changed the text";
        PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Text"));
        return true;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {

    }

    public event PropertyChangedEventHandler PropertyChanged;
}

Then in xaml bind the Header to the MyCommandClass's Text property.

0

精彩评论

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