3
votes

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

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

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

but I don't know how to do it.

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");
         }
           );

        }
    }

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

2
Why don't you pass the textbox itself and read the text in your method ? - Nawed Nabi Zada
I want to recongnize the Enter key, or maybe F1,F2, Esc , if just want to read the text, the Binding Propertychange will update the string OpenFPth whenever the text change. - yu yang Jian
Check my answer @yuyangJian - Nawed Nabi Zada
@NawedNabiZada, that would be wrong to do. The VM should remain UI agnostic, and not know about any TextBox. - Shimmy Weitzhandler
Also using the MessageBox in VM is wrong, you wanna wrap that in a commonly shared interface instead, or use one of your MVVM frameworks solutions. - Shimmy Weitzhandler

2 Answers

0
votes

You can pass the Text box it self or just the text. I don't know what you want to do with it, but if you have to interact with the textbox then I would suggest to pass the text box it self:

Passing the text:

<i:Interaction.Triggers>
   <i:EventTrigger EventName="KeyDown">
     <i:InvokeCommandAction Command="{Binding OpenFCmd}" 
        CommandParameter="{Binding ElementName=Box, Path=Text}" />
   </i:EventTrigger>
</i:Interaction.Triggers>

Passing the textBox:

<i:Interaction.Triggers>
   <i:EventTrigger EventName="KeyDown">
     <i:InvokeCommandAction Command="{Binding OpenFCmd}" 
        CommandParameter="{Binding ElementName=Box}" />
   </i:EventTrigger>
</i:Interaction.Triggers>

Edit:

Whit the above code you can create an eventhandler in your code:

void OpenFCmn_OnClick(object obj)
    {
        var tb = (TextBox) obj;

        tb.KeyDown += TbOnKeyDown;

    }

    private void TbOnKeyDown(object sender, KeyEventArgs keyEventArgs)
    {
        MessageBox.Show(keyEventArgs.Key.ToString());
        keyEventArgs.Handled = true;

    }
0
votes

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>