Home > DeveloperSection > Forums > Textbox KeyBinding with AcceptsReturn = true
Simons Hood
Simons Hood

Total Post:70

Posted on    September-04-2013 3:23 AM


 1 Reply(s)
 2177  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.

Sumit Kesarwani

Total Post:378

Posted on    September-04-2013 7:13 AM

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}"/>

Don't want to miss updates? Please click the below button!

Follow MindStick