tags:

views:

96

answers:

1

I have context menu in XAML with the following MenuItem:

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

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

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

+1  A: 

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.

Eric Dahlvang