WPF custom command in context menu are disabled until any button clicked

后端 未结 4 950
无人及你
无人及你 2020-12-31 03:48

I have a custom command and I try to execute them from the context menu, but they are always displayed as disabled unless I click any button on the UI (buttons do not have a

4条回答
  •  生来不讨喜
    2020-12-31 04:00

    There is probably some change "behind the scenes" that would normally enable the commands, but the view is not aware of this change. One would need to see the Command-implementations to give more precise hints.

    You can either make anything that changes your command-enable-state notify the view or manually trigger a command-refresh via CommandManager.InvalidateRequerySuggested(), for example when the context-menu opens.

    WPF ICommands work that way; they requery their CanExecute function whenever something in the view changes (e.g. PropertyChanged-event is fired or a button is clicked), but they don't requery if they have no reason to.

提交回复
热议问题