gpt4 book ai didi

wpf - 快捷键问题 - 有时不起作用 - 焦点问题?

转载 作者:行者123 更新时间:2023-12-04 22:28:49 26 4
gpt4 key购买 nike

我有一个 WPF 应用程序,主窗口中有一个“ContentControl”,它有一个 TreeView 、菜单和一个数据网格。当我打开文件时,它们会被填充。

我花了大约一天的时间试图让键绑定(bind)工作。绑定(bind)在用户控件中,但我读过 (http://blogs.southworks.net/geoff/2011/03/15/wpfshortcutkeys/),除非你把它放在主窗口中,否则你可能会遇到问题(这是我拥有的一个简单的应用程序)。

所以今天我在主窗口中放了一个测试命令。好一点 - 当我启动应用程序时,快捷方式有效,但在使用文件打开(或其他操作)后,它会停止。我用过“Snoop”,可以看到焦点没有设置到任何元素上——当它不工作时它是“null”,当它工作时它在启动时也是“null”。

当我按下快捷键时,“Snoop”在事件选项卡中什么也没有显示。如果我单击一个控件以使某物具有焦点,则事件会再次出现。

就好像某些东西得到了不应该的焦点。

我在主窗口中有一些教科书 XAML -

    <Window ... Height="400" Width="772" Focusable="False" IsTabStop="False">
<Window.Resources>
<DataTemplate DataType="{x:Type vm:MainWindowContentViewModel}">
<local:MainWindowContent/>
</DataTemplate>
</Window.Resources>
<Window.CommandBindings>
<CommandBinding Command="Close"
CanExecute="CloseCanExecute"
Executed="CloseExecuted" />
</Window.CommandBindings>

<Window.InputBindings>
<KeyBinding Key="A"
Modifiers="Control"
Command="Close" />
</Window.InputBindings>

<ContentControl x:Name="_contentControl"/>
</Window>

后面的代码也很无聊(在某处从互联网上下载!)。
private void HelpCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
e.Handled = true;
}

private void HelpExecuted(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Hey, I'm some help.");
e.Handled = true;
}

还有一个复杂之处在于我的大多数命令都使用 ICommand 的东西,但我添加了这个简单的帮助命令来尝试简化事情,因为它在焦点上有相同的问题。 (我使用 Keybinding a RelayCommand 中的代码来解决这个问题!)

有什么提示吗?

谢谢。

彼得

最佳答案

我会在 Contentcontrol 上设置 IsTabstop=false - 它默认为 true,您无法看到它何时具有焦点,因为它没有样式/控件模板。

当您的程序启动时,内容控件似乎具有焦点......

此外,我会在窗口上删除 Focusable=false - 这可能是导致焦点模糊的原因。

关于wpf - 快捷键问题 - 有时不起作用 - 焦点问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6911396/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com