ReactiveCommand pass 命令参数 [英] ReactiveCommand pass Command Parameter

查看:51
本文介绍了ReactiveCommand pass 命令参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用command来实现textbox中的KeyDown事件,

I want to use command to realize the KeyDown Event in textbox,

我希望让命令可以识别 KeyDown Event 中的 KeyEventArgs 等哪些键输入并做一些其他的事情,

I wnat to let the command can recognize which key input such as KeyEventArgs do in KeyDown Event and do some other things,

所以我想将命令参数传递给 ReactiveCommand(就像 Eventargs 在 Event Method 中所做的那样),

So I want to pass command parameter into ReactiveCommand (just like Eventargs do in Event Method),

但我不知道该怎么做.

我有 WPF Xaml 代码:

I have WPF Xaml code :

<Window x:Class="NameSpaceTest.OpenFileW"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:command="http://www.galasoft.ch/mvvmlight"
        Title="OpenFileWVieModel" Height="50" Width="200" WindowStyle="None">
    <Grid>
        <TextBox Name="Box" Text="{Binding OpenFPth, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="KeyDown">
                    <i:InvokeCommandAction Command="{Binding OpenFCmd}" CommandParameter=""/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </TextBox>
    </Grid>
</Window>

两个类代码首先是视图,其次是视图模型:

and two class code first is the view, second the viewmodel:

   public partial class OpenFileW : Window
    {
        OpenFileWVieModel OFVM  = new OpenFileWVieModel();
        public OpenFileW()
        {
            this.DataContext = OFVM;
            InitializeComponent();
        }
    }


    public class OpenFileWVieModel : ReactiveObject
    {
        public string OpenFPth { get { return _openFpth; } set { this.RaiseAndSetIfChanged(ref _openFpth, value); } }
        private string _openFpth;

        public IReactiveCommand OpenFCmd { get; set; }

        public OpenFileWVieModel()
        {
          var TextChange = this.WhenAny(
                    x => x.OpenFPth,
                x =>
                {
                    MessageBox.Show("HI");
                    return true;
                });

       OpenFCmd = ReactiveCommand.CreateAsyncTask(
        TextChange,  //CanExecute
         async _ =>//Execute
         {
             MessageBox.Show("HI");
         }
           );

        }
    }

接下来要做什么将文本框 KeyInput 作为参数传递给命令?

What to do next to pass the textbox KeyInput as parameter to the command?

推荐答案

将 KeyDown 事件绑定到视图模型的命令并设置 PassEventArgsToCommand="True" 这会将 KeyDown 事件的参数传递给您的命令.

Bind the KeyDown event to the view model's command and set PassEventArgsToCommand="True" this will pass the KeyDown event's arguments to your command.

<TextBox  VerticalAlignment="Top" Width="120">
     <i:Interaction.Triggers>
           <i:EventTrigger EventName="KeyDown">
                  <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding KeyDownCommand}" PassEventArgsToCommand="True" />
           </i:EventTrigger>
     </i:Interaction.Triggers>
</TextBox>

这篇关于ReactiveCommand pass 命令参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆