[英]Adding “close tab” keybinding to WPF TabItem in C#
I've implemented this modified TabItem to add a "Close" button to each tab of a WPF TabControl: 我已经实现了此修改过的TabItem,以向WPF TabControl的每个选项卡添加“关闭”按钮:
http://www.codeproject.com/Articles/281402/Closable-TabItem-in-WPF http://www.codeproject.com/Articles/281402/Closable-TabItem-in-WPF
Everything works very well, but I cannot figure out for the life of me how to add a KeyBinding for Ctrl+W to serve as an alternate way of closing tabs. 一切工作都很好,但是我一生都无法解决如何为Ctrl + W添加KeyBinding来作为关闭选项卡的另一种方式。 I've tried adding to the CloseTab.cs class as well as my main window class with no luck.
我尝试添加到CloseTab.cs类以及我的主窗口类没有任何运气。
I was using this for reference: 我正在使用它作为参考:
Keyboard shortcuts in WPF WPF中的键盘快捷键
...but I don't really understand how to add the shortcut. ...但是我不太了解如何添加快捷方式。
I haven't really tested this, but maybe instead of using InputGestures
(since you can't get that to work I guess), maybe you can alternatively bind to a command and check the Keyboard
for currently pressed keys? 我还没有真正测试过,但是也许不使用
InputGestures
(因为我想您不能InputGestures
正常工作),也许您可以绑定到命令并检查Keyboard
上当前按下的键吗?
So inside the xaml where the ClosableTabItem
is defined: 因此,在定义
ClosableTabItem
的xaml中:
(you'll need to add a reference to System.Windows.Interactivity
) (您需要添加对
System.Windows.Interactivity
的引用)
xmlns:local="clr-namespace:TestDemo"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
<local:ClosableTabItem Header="TabItem 1">
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyDown">
<i:InvokeCommandAction Command="{Binding KeyDownCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</local:ClosableTabItem>
Then the command: 然后命令:
private RelayCommand _KeyDownCommand;
public ICommand KeyDownCommand
{
get
{
if (this._KeyDownCommand == null)
this._KeyDownCommand = new RelayCommand(param => this.CheckKeysDown());
return this._KeyDownCommand;
}
}
And the method: 和方法:
private void CheckKeysDown()
{
if (Keyboard.IsKeyDown(Key.W) && ((Keyboard.Modifiers & ModifierKeys.Control) > 0))
{
// Ctrl + W was pressed
}
}
EDIT: To expand on my answer, I found this pretty useful implementation as well: http://joyfulwpf.blogspot.com/2009/05/mvvm-commandreference-and-keybinding.html 编辑:为了扩大我的答案,我也发现了这个非常有用的实现: http : //joyfulwpf.blogspot.com/2009/05/mvvm-commandreference-and-keybinding.html
You can use CommandReference
in the above link's implementation to execute when a KeyBinding
is satisfied! 您可以在上述链接的实现中使用
CommandReference
在满足KeyBinding
时执行!
EDIT2: To do this in the code behind (for dynamically added tab items): EDIT2:为此,请在后面的代码中(对于动态添加的标签项):
System.Windows.Interactivity.EventTrigger trigger =
new System.Windows.Interactivity.EventTrigger();
trigger.EventName = "KeyDown";
System.Windows.Interactivity.InvokeCommandAction keyDownAction =
new System.Windows.Interactivity.InvokeCommandAction();
keyDownAction.Command = KeyDownCommand;
trigger.Actions.Add(keyDownAction);
trigger.Attach(yourTabItem);
EDIT3: Ok, I just got the code myself and played around to figure out a fix for you. EDIT3:好的,我自己得到了代码,然后四处寻找解决方法。 The way the
ClosableTabItem
class removes the tab is with the command StateChange
. ClosableTabItem
类删除选项卡的方式是使用StateChange
命令。 And I just added a KeyBinding
to a tab item I made in the code behind and it worked. 我只是将
KeyBinding
添加到了我在后面的代码中创建的选项卡项中,并且它起作用了。
So I did this: 所以我这样做:
ClosableTabItem myTab = new ClosableTabItem();
myTab.InputBindings.Add(new KeyBinding(ClosableTabItem.StateChange, Key.W, ModifierKeys.Control));
So when you dynamically create your tabs, add this input binding above and have it hook up to the StateChange
command. 因此,当您动态创建选项卡时,请在上方添加此输入绑定,并将其连接到
StateChange
命令。 You might have to click the tab or tab area to get it in focus for the key binding to work. 您可能必须单击选项卡或选项卡区域才能使其聚焦,才能使键绑定生效。 I noticed that during my testing.
我在测试期间注意到了这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.