如何使用 ICommand 在 MVVM 中触发 KeyUp 事件

How to trigger KeyUp Event in MVVM using ICommand

我创建了这个简单的视图:

现在,当我在光标位于这两个文本框之一内时按下 RETURN 键时,我希望 "Suchen" = SEARCH 按钮触发(KeyUp 事件)。

我知道如何在后面的代码中轻松执行此操作,但我想在 MVVM(在我的视图模型 class 中)中使用 ICommand 执行此操作。在后面的代码中,我使用了(自动生成的)KeyEventArgs 参数。

我在 MVVM 中使用 ICommand 进行了尝试,但 Command 方法给我一个错误,声称我需要为 KeyEventArgs 参数实例化一个对象。在后面的代码中(非 mvvm-like)我不需要实例化任何东西,因为 KeyEventArgs 参数是 "autogenerated" 就像方法一样。所以我不必担心这个。

如何使 KeyUp 事件在我的 MVVM 项目中起作用? 为了回答这个问题,我为您提供了以下缩短的代码:

XAML-查看:

 <StackPanel Height="423" VerticalAlignment="Bottom">
        <Label Name="lblArtikelbezeichnung" Content="Artikelbezeichnung:" Margin="20, 20, 20, 0"></Label>
        <TextBox Name="txtArtikelbezeichnung" 
                 Width="Auto" 
                 Margin="20, 0, 20, 0"
                 IsEnabled="{Binding BezEnabled}"
                 Text="{Binding BezText}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="TextChanged">
                    <i:InvokeCommandAction Command="{Binding TextChangedBez}" />
                </i:EventTrigger>
                <i:EventTrigger EventName="KeyUp">
                    <i:InvokeCommandAction Command="{Binding KeyUpBez}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </TextBox>
        <!--TextChanged="txtArtikelbezeichnung_TextChanged" 
                 KeyUp="txtArtikelbezeichnung_KeyUp"-->
        <Label Name="lblLieferant" Content="Lieferant:" Margin="20, 0, 20, 0"></Label>
        <TextBox Name="txtLieferant" 
                 Width="Auto" 
                 Margin="20, 0, 20, 0"
                 IsEnabled="{Binding LiefEnabled}"
                 Text="{Binding LiefText}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="TextChanged">
                    <i:InvokeCommandAction Command="{Binding TextChangedLief}" />
                </i:EventTrigger>
                <i:EventTrigger EventName="KeyUp">
                    <i:InvokeCommandAction Command="{Binding KeyUpLief}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </TextBox>
            <!--TextChanged="txtLieferant_TextChanged" 
                 KeyUp="txtLieferant_KeyUp"-->
        <Button Name="btnSuchen" 
                Content="Suchen" 
                Width="100" Height="25" 
                Margin="20, 10,240, 10" 
                Command="{Binding GefilterteSuche}">
        </Button>
...
<StackPanel>

隐藏代码:

    using System.Windows;

    namespace Lieferscheine
    {
        /// <summary>
        /// Interaktionslogik für artikelHinzu.xaml
        /// </summary>
    public partial class artikelHinzu : Window
    {

        public artikelHinzu()
        {
            InitializeComponent();
            DataContext = new ArtikelHinzuViewModel();
        }     
    }
}

查看模型:

    public class ArtikelHinzuViewModel : INotifyPropertyChanged
    {              
//ICommands (shortened)
        public ICommand GefilterteSuche => new DelegateCommand<object>(SucheArtikel);                     
        public ICommand KeyUpLief => new DelegateCommand<KeyEventArgs>(KeyUpLieferant);       
        public ICommand KeyUpBez => new DelegateCommand<KeyEventArgs>(KeyUpBezeichnung);

 //INotifyPropertyChanged      
        public event PropertyChangedEventHandler PropertyChanged;
        public virtual void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        //Konstruktor
        public ArtikelHinzuViewModel()
        {

        }

//ICommand methods (shortened for reasons of simplicity)        

         //KeyUp Events (THIS PART IS MY PROBLEM)      
        private void KeyUpBezeichnung(KeyEventArgs e) //the argument is obligatory but it does not have an instantiated object which is why an error fires...
        {
//since I need to create an object for KeyEventArgs I tried this but it is useless...
        /*e = new KeyEventArgs(Keyboard.PrimaryDevice,
        Keyboard.PrimaryDevice.ActiveSource,
        0, Key.Back);
//I need to access this e.Key property but don't know how in my case! That is the actual problem...
            if (e.Key == Key.Return)
            {
                object o = new object();
                SucheArtikel(o);
            }
            */
        }
//same problem here as above...
        private void KeyUpLieferant(KeyEventArgs e)
        {
            /*
        e = new KeyEventArgs(Keyboard.PrimaryDevice,
        Keyboard.PrimaryDevice.ActiveSource,
        0, Key.Back);

            if (e.Key == Key.Return)
            {
                object o = new object();
                SucheArtikel(o);
            }
             */
        }

    }

使用 InputBindings 更容易:

<TextBox>
    <TextBox.InputBindings>
        <KeyBinding Key="Enter" Command="{Binding SearchCommand}" />
    </TextBox.InputBindings>
</TextBox>