0
votes

I am struggling with a thing like i have a textbox

<TextBox Name="FilterInputText" Visibility="{Binding VisibiltyAttr}" Width="500" Height="30" Text="{Binding InputText}" HorizontalAlignment="Left" Margin="5">
                    <TextBox.InputBindings>
                        <KeyBinding Command="{Binding EnterCommand}" Key="Enter" />
                        <KeyBinding Command="{Binding DownSelectionCommand}" Key="Down" />
                    </TextBox.InputBindings>
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="KeyUp">
                            <i:InvokeCommandAction  Command="{Binding CompleteCommand}"  CommandParameter="{Binding Text, ElementName=InputText}" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </TextBox> 

i have a event triggered here with something input inside textbox with "KeyUp" event. now i am working for autocomplete textbox so i showed suggestions on typing in a list box that is working fine. so i need to bind down key to bind with this text box so that after showing suggestions user can press down key and select his desired option from there. It will work fine for keybinding for down key.

The problem is with event keyup because any key press inside textbox then this event triggerd. now i am sending textbox value as command parmeter but i also need to send keyeventargs with comamnd pararmter so that i can findout which key is pressed and when downkey comesup i will not further execute the method.

So how i can i pass both textbox value and keyeventargs as command parameter, i am strictly following mvvm pattern.

1
Do you need to pass the TextBox value as a command parameter? Don't you already have it in your view model as InputText?redcurry

1 Answers

0
votes

By default, InvokeCommandAction passes the event args if you do not specify a command parameter. Also, you're textbox is bound to a property on your view model. So, if you change the the code as follows:

<TextBox Name="FilterInputText" Visibility="{Binding VisibiltyAttr}" Width="500" Height="30" Text="{Binding InputText, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Margin="5">
  <TextBox.InputBindings>
       <KeyBinding Command="{Binding EnterCommand}" Key="Enter" />
       <KeyBinding Command="{Binding DownSelectionCommand}" Key="Down" />
  </TextBox.InputBindings>
  <i:Interaction.Triggers>
        <i:EventTrigger EventName="KeyUp">
            <i:InvokeCommandAction  Command="{Binding CompleteCommand}" />
        </i:EventTrigger>
   </i:Interaction.Triggers>
</TextBox> 

And then make sure your command's execute method has the right event args type for a parameter, then when the CompleteCommand is invoked you should have the event args as the parameter, and you should be able to check the InputText property on your view model for the text value.

Notice that I added "UpdateSourceTrigger=PropertyChanged" to the binding on the TextBox Text property. That will cause the property to be updated in the view model every single time the user types.

See the following link for my source on InvokeCommandAction's default behavior:

https://github.com/Microsoft/XamlBehaviors/issues/126