WPF: Change the text of menuitem on context menu
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
, MenuItem
s 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.
精彩评论