WPF树视图键绑定Ctrl+Down
我试图在树状视图中将Ctrl+Down作为自定义快捷方式处理。我尝试了以下代码:WPF树视图键绑定Ctrl+Down,wpf,treeview,keyboard-shortcuts,Wpf,Treeview,Keyboard Shortcuts,我试图在树状视图中将Ctrl+Down作为自定义快捷方式处理。我尝试了以下代码: <Window x:Class="WpfTreeIssue.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microso
<Window x:Class="WpfTreeIssue.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfTreeIssue"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"
x:Name="_this">
<Grid DataContext="{Binding ElementName=_this}">
<TreeView>
<TreeView.InputBindings>
<KeyBinding
Key="Down" Modifiers="Ctrl"
Command="{Binding ControlDownCommand}" />
<KeyBinding
Key="A"
Command="{Binding ControlDownCommand}" />
</TreeView.InputBindings>
<TreeViewItem Header="Level 1" IsExpanded="True">
<TreeViewItem Header="Level 2.1" />
<TreeViewItem Header="Level 2.2" IsExpanded="True">
<TreeViewItem Header="Level 3.1" />
<TreeViewItem Header="Level 3.2" />
</TreeViewItem>
<TreeViewItem Header="Level 2.3" />
</TreeViewItem>
</TreeView>
</Grid>
如果我按住Ctrl键并按下行控制台。WriteLineDown箭头;永不被击中向下箭头事件未发送。如果我只按下向下键,该行将被点击,但ctrl修饰符未设置。TreeView控件中有一个处理ctrl+down的代码,因此您的绑定无法工作,因为keys事件已被处理。您需要覆盖TreeView上的OnKeyDown来完成您自己的工作
更新:这是一个测试样本
protected override void OnKeyDown(KeyEventArgs e)
{
if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
{
if (System.Windows.Input.Key.Down == e.Key)
{
System.Diagnostics.Trace.WriteLine("Ctlr+Down");
e.Handled = true;
}
}
if (!e.Handled)
{
base.OnKeyDown(e);
}
}
我尝试了一下,但是OnKeyDown事件不会触发向下箭头键。根据需要,我需要使用PreviewKeyDown来处理箭头键,箭头键本身会触发Down,但如果按下Down+ctrl,则不会。我可能需要结合使用预览/向上/向下和轨迹状态。您需要在控件中重写OnKeyDown方法,因为只有在未处理关键点时才会触发事件。或者,正如你所说,PreviewXXX就是这个地方。您可能希望使用Keyboard.Modifiers.HasFlagModifierKeys.Control来检查控制键的状态。已尝试覆盖查看更新到问题-我似乎只能访问Ctrl键或向下键。。。。不是同时两个:你尝试了静态属性键盘。修改器而不是键盘设备?是的,但问题是在那之前。。如果按住Ctrl键,则如果e.Key==Key.down,则OnpreviewKeyDown或OnKeyDown事件都不会通过。它们会为Ctrl键触发一次,然后在我放开Ctrl键之前,不要为down键再次触发。
public class CustomTreeView : TreeView
{
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.Key == Key.Down)
{
Console.WriteLine("Down arrow");
if ((e.KeyboardDevice.IsKeyDown(Key.RightCtrl) || e.KeyboardDevice.IsKeyDown(Key.LeftCtrl)))
{
Console.WriteLine("TEST");
e.Handled = true;
}
}
base.OnKeyDown(e);
}
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
if (e.Key == Key.Down)
{
Console.WriteLine("Down arrow");
if ((e.KeyboardDevice.IsKeyDown(Key.RightCtrl) || e.KeyboardDevice.IsKeyDown(Key.LeftCtrl)))
{
Console.WriteLine("TEST");
e.Handled = true;
}
}
base.OnPreviewKeyDown(e);
}
}
protected override void OnKeyDown(KeyEventArgs e)
{
if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
{
if (System.Windows.Input.Key.Down == e.Key)
{
System.Diagnostics.Trace.WriteLine("Ctlr+Down");
e.Handled = true;
}
}
if (!e.Handled)
{
base.OnKeyDown(e);
}
}