wpf C# - 如何在 MVVM 中处理 XAML 键盘?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20428526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# - How to handle XAML Keyboard in MVVM?
提问by Elfayer
Untill now, I'm using the .xaml.cs to handle the inputs.
到现在为止,我使用 .xaml.cs 来处理输入。
Here is my xaml head code :
这是我的 xaml 头代码:
<Window x:Class="My_Windows_App.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:My_Windows_App.ViewModel"
Title="MainWindow" Height="600" Width="900"
Keyboard.KeyDown="keyDownEventHandler" Keyboard.KeyUp="keyUpEventHandler">
And here is a part of the MainWindow.xaml.cs code :
这是 MainWindow.xaml.cs 代码的一部分:
public partial class MainWindow : Window
{
private bool pushToTalk;
public void keyDownEventHandler(object sender, KeyEventArgs e)
{
if (e.Key == Key.LeftCtrl)
pushToTalk = true;
}
public void keyUpEventHandler(object sender, KeyEventArgs e)
{
if (e.Key == Key.LeftCtrl)
pushToTalk = false;
}
}
How can I implent the same thing in MVVM ? Because as far as I understand, we can not bind a method, only properties, right ?
我怎样才能在 MVVM 中实现同样的事情?因为据我所知,我们不能绑定方法,只能绑定属性,对吧?
采纳答案by Federico Berasategui
Input handling is a View concern, not a ViewModel concern, why would you want to move that to the ViewModel?
输入处理是 View 的问题,而不是 ViewModel 的问题,为什么要将其移至 ViewModel?
instead, delegate the application / business logic to the ViewModel, while keeping the Keyboard Input handling in the View:
相反,将应用程序/业务逻辑委托给 ViewModel,同时在 View 中保留键盘输入处理:
public partial class MainWindow : Window
{
private MyViewModel ViewModel;
public MainWindow()
{
ViewModel = new MyViewModel();
}
public void keyDownEventHandler(object sender, KeyEventArgs e)
{
if (e.Key == Key.LeftCtrl)
ViewModel.PushToTalk = true;
}
public void keyUpEventHandler(object sender, KeyEventArgs e)
{
if (e.Key == Key.LeftCtrl)
ViewModel.PushToTalk = false;
}
}
Notice how I moved the PushToTalkproperty to the ViewModel, because that is really part of the application logic and not the UI, while keeping Keyboard events at the View level. This does not break MVVM because you're not mixing UI and application logic, you're just placing things where they really belong.
请注意我如何将PushToTalk属性移动到 ViewModel,因为这实际上是应用程序逻辑的一部分,而不是 UI,同时将键盘事件保留在视图级别。这不会破坏 MVVM,因为您没有混合 UI 和应用程序逻辑,您只是将东西放在它们真正属于的地方。
回答by Noctis
Seems you got your answer, but here's another take on a solution.
You can tackle the keyboard events with i:interactionand InputBindings. Some examples using a DatePickerwith interaction would be:
似乎你得到了答案,但这里有另一种解决方案。
您可以使用i:interaction和处理键盘事件InputBindings。使用DatePickerwith 交互的一些示例是:
<DatePicker Name="fancy_name_here" SelectedDate="{Binding your_date_property_here}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewKeyDown">
<i:InvokeCommandAction Command="{Binding Path=TestMeCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</DatePicker>
(Do note that you'll need the next line in your window definitions for the i:Interactionto work:)
(请注意,您需要窗口定义中的下一行i:Interaction才能工作:)
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
and for the input binding:
对于输入绑定:
<DatePicker Name="fancy_name_here" SelectedDate="{Binding your_date_property_here}">
<DatePicker.InputBindings>
<KeyBinding Key="Up" Modifiers="Shift" Command="{Binding your_command_name}" />
<KeyBinding Key="Up" Modifiers="Alt" Command="{Binding TestMeCommand}" />
<KeyBinding Key="Up" Command="{Binding TestMeCommand}" />
</DatePicker.InputBindings>
</DatePicker>
Feel free to have a look at my other answer, it discusses i:interactionand InputBindings, and points in return to another article about handling key events: up/down on datepicker, and discusses the code behind / mvvm approaches.
请随意查看我的其他答案,它讨论了i:interaction和InputBindings,并指向另一篇关于处理关键事件的文章:在 datepicker上向上/向下,并讨论了 / mvvm 方法背后的代码。
Hope you'll find them useful.
希望你会发现它们很有用。
回答by SurfingSanta
The following works for handling the "Enter" key in a TextBox:
以下适用于处理 TextBox 中的“Enter”键:
<TextBox Text="{Binding UploadNumber, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<TextBox.InputBindings>
<KeyBinding
Key="Enter"
Command="{Binding FindUploadCommand}"
CommandParameter="{Binding Path=Text, RelativeSource={RelativeSource AncestorType={x:Type TextBox}}}" />
</TextBox.InputBindings>

