TEXTBOX KEYBINDING WITH ACCEPTSRETURN = TRUE

Simons Hood

Total Post:70

Points:494
Posted by  Simons Hood
C#
 2272  View(s)
Ratings:
Rate this:

Basically I have a textbox which will accept return when a checkbox is unchecked and when it's checked I want the textbox to react on KeyBinding I wrote.

<TextBox AcceptsReturn="{Binding IsChecked, ElementName=EnterCheckbox, Converter={StaticResource InvertBooleanConverter}}" >

     <TextBox.InputBindings>

           <KeyBinding Key="Enter" Command="{Binding CmdEnterPressed}"/>

     </TextBox.InputBindings>

</TextBox >

Now in any cases, despite of fact that TextBox.AcceptsReturn is set to True when I press [Return] KeyBinding is firing and I want not to fire it but to go to next line of TB.

  1. Sumit Kesarwani

    Post:378

    Points:2694
    Re: Textbox KeyBinding with AcceptsReturn = true

    Hi Samuel,


    You can implement CanExecute for that command like this:

    public bool CanExecute(object parameter)

    {

        bool acceptReturns = (bool)parameter;

        return !acceptReturns;

    }

    And in your XAML (as far as CommandParameter is used for both Execute and CanExecute methods):

    <KeyBinding Key="Enter" Command="{Binding CmdEnterPressed}"

                            CommandParameter="{Binding RelativeSource={RelativeSourceMode=FindAncestor, AncestorType={x:Type TextBox}}, Path=AcceptsReturn}"/>

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!