繁体   English   中英

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

[英]How to trigger KeyUp Event in MVVM using ICommand

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

在此处输入图片说明

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

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

我使用 ICommand 在 MVVM 中尝试过,但是 Command 方法给了我一个错误,声称我需要为 KeyEventArgs 参数实例化一个对象。 在后面的代码(非 mvvm 类)中,我不需要实例化任何东西,因为 KeyEventArgs 参数就像方法一样是“自动生成的”。 所以我不必担心。

如何使 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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM