Textbox KeyBinding with AcceptsReturn = true

Total Post:70


 3056  View(s)
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}}" >


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


</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. 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
                            CommandParameter="{Binding RelativeSource={RelativeSourceMode=FindAncestor, AncestorType={x:Type TextBox}}, Path=AcceptsReturn}"/>

      Modified On Mar-31-2018 12:54:39 AM


Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.