10/05/01 10:52:45
2010を使い.NET4でWPFを始めてみました
ググりながら試行錯誤してコマンドとコンテキストメニューを繋ごうとしています
キー操作だと正常に動作し、PlayPause_Executeが呼ばれるのですが、
コンテキストメニューは見出しやキーの表示は合っていますが、グレー表示のままで選択できません
何が悪いのでしょうか?
タイマで定期的にCommandManager.InvalidateRequerySuggested();してみても変わらないようですし
キーを押したときにはCheckCanExecuteCommandが呼ばれますが、それ以外では呼ばれないようです
public class Commands
{
public static RoutedUICommand PlayPause = new RoutedUICommand("再生/一時停止", "PlayPause", typeof(Commands),
new InputGestureCollection { new KeyGesture(Key.Space) });
}
XAMLで(はしょっています)
<Window xmlns:local="clr-namespace:hoge">
<StatusBar.ContextMenu>
<ContextMenu>
<MenuItem Command="{x:Static local:Commands.PlayPause}" IsEnabled="True" />
</ContextMenu>
</StatusBar.ContextMenu>
C#側コンストラクタで
CommandBindings.Add(new CommandBinding(Commands.PlayPause, PlayPause_Execute, CheckCanExecuteCommand));
private void CheckCanExecuteCommand(Object sender, CanExecuteRoutedEventArgs e) {
e.CanExecute = true;
}