开发者

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, 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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜