即使命令被禁用,WPF输入手势也会从TextInput处理程序中隐藏字符

即使命令被禁用,WPF输入手势也会从TextInput处理程序中隐藏字符,wpf,command,textinput,Wpf,Command,Textinput,我有这个: public static RoutedUICommand SplitLine = new RoutedUICommand("Split Line", "SplitLine", typeof(TabItem), new InputGestureCollection( new InputGesture[] { new KeyGesture(Key.OemPeriod) })); 这允许我通过点击OemPeriod来运行SplitLin

我有这个:

public static RoutedUICommand SplitLine = 
    new RoutedUICommand("Split Line", "SplitLine", typeof(TabItem), 
        new InputGestureCollection(
           new InputGesture[] { new KeyGesture(Key.OemPeriod) }));
这允许我通过点击OemPeriod来运行SplitLine命令。到目前为止效果很好

接下来,我有一个在某些情况下正确禁用SplitLine的选项:

static void splitline_CanExecute(object sender, CanExecuteRoutedEventArgs e) {
    e.CanExecute = FalseForSomeReason();
}
问题是我也有这段代码,当SplitLine无法执行时,这段代码很有用:

void myControl_TextInput(object sender, TextCompositionEventArgs e) {
    // this code never gets an OemPeriod;
}
问题是TextInput处理程序从未看到OemPeriod。其他角色没问题。我也尝试过TextInput、PreviewtInput、Keydown——OemPeriod没有通过任何这些功能


当SplitLine命令被禁用时,有没有办法通过文本输入或其他方式查看控件中的OemPeriod?

正确的解决方案是在CanExecute为false时在CanExecute方法中设置ContinueRouting标志:

static void splitline_CanExecute(object sender, CanExecuteRoutedEventArgs e) {
    e.CanExecute = FalseForSomeReason();
    if (e.CanExecute == false) e.ContinueRouting = true;   // <=== Add this!
}

这允许OemPeriod继续寻找一些东西来处理它,在本例中是TextInput处理程序。否则,当CanExecute拒绝执行时,OemPeriod的所有处理都会停止。

我的解决方法是在SplitLine不可执行时删除keybinding,然后在SplitLine再次可用时将其添加回。