WPF Code Creation of input Binding
-
I would like to know how to implement the following xaml in code, specifically the input binding to a command.
<Controls:TextBox Text="{Binding Path=SearchText, UpdateSourceTrigger=PropertyChanged}">
Controls:TextBox.InputBindings
<KeyBinding Key="Return" Command="{Binding Path=SearchCommand}"/>
</Controls:TextBox.InputBindings>
</Controls:TextBox>The reason for the question is i am looking to inherit from some of the default windows controls, and have some standard behaviour for my inherited control. I wish for the return and escape keys to execute a commands in my ViewModel which will be the DataContext for the Inherited Control. The return key would trigger a search to be performed, and the escape key would clear the control to its default value, any help would be greatly appreciated.
-
I would like to know how to implement the following xaml in code, specifically the input binding to a command.
<Controls:TextBox Text="{Binding Path=SearchText, UpdateSourceTrigger=PropertyChanged}">
Controls:TextBox.InputBindings
<KeyBinding Key="Return" Command="{Binding Path=SearchCommand}"/>
</Controls:TextBox.InputBindings>
</Controls:TextBox>The reason for the question is i am looking to inherit from some of the default windows controls, and have some standard behaviour for my inherited control. I wish for the return and escape keys to execute a commands in my ViewModel which will be the DataContext for the Inherited Control. The return key would trigger a search to be performed, and the escape key would clear the control to its default value, any help would be greatly appreciated.
Sounds like the perfect use for attached behaviors! :-D
-
Sounds like the perfect use for attached behaviors! :-D
The following appears to impelemtn what you suggested, to be honest i'm not sure that i understand the need/benifit to use an attached behaviour insted of inheriting from a control.
public static class LookupBehaviour { public static Boolean? GetEscapeToClear(ComboBox comboBox) { return (Boolean?) comboBox.GetValue(EscapeToClearProperty); } public static void SetEscapeToClear(ComboBox comboBox, Boolean? value) { comboBox.SetValue(EscapeToClearProperty, value); } public static readonly DependencyProperty EscapeToClearProperty = DependencyProperty.Register("EascapeToClear", typeof (Boolean?), typeof (LookupBehaviour), new UIPropertyMetadata(null, OnEscapeToCearChanged)); private static void OnEscapeToCearChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (!(d is ComboBox)) return; var cmb = d as ComboBox; if ((e.NewValue is Boolean?) && (e.NewValue as Boolean?).HasValue) { var escapeBindingCommand = new KeyBinding(ApplicationCommands.NotACommand, new KeyGesture(Key.Escape)); var escapeBidning = new Binding("EscapeCommand"); BindingOperations.SetBinding(escapeBindingCommand, InputBinding.CommandProperty, escapeBidning); cmb.InputBindings.Add(escapeBindingCommand); } } }
One thing that i didn't like was when creating the key binding that you have to specify a tempory command, ApplicationCommands.NotACommand, and then bind over the top of it, if there is any way to avoid that please let me know. Thanks for the origional reply.
-
The following appears to impelemtn what you suggested, to be honest i'm not sure that i understand the need/benifit to use an attached behaviour insted of inheriting from a control.
public static class LookupBehaviour { public static Boolean? GetEscapeToClear(ComboBox comboBox) { return (Boolean?) comboBox.GetValue(EscapeToClearProperty); } public static void SetEscapeToClear(ComboBox comboBox, Boolean? value) { comboBox.SetValue(EscapeToClearProperty, value); } public static readonly DependencyProperty EscapeToClearProperty = DependencyProperty.Register("EascapeToClear", typeof (Boolean?), typeof (LookupBehaviour), new UIPropertyMetadata(null, OnEscapeToCearChanged)); private static void OnEscapeToCearChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (!(d is ComboBox)) return; var cmb = d as ComboBox; if ((e.NewValue is Boolean?) && (e.NewValue as Boolean?).HasValue) { var escapeBindingCommand = new KeyBinding(ApplicationCommands.NotACommand, new KeyGesture(Key.Escape)); var escapeBidning = new Binding("EscapeCommand"); BindingOperations.SetBinding(escapeBindingCommand, InputBinding.CommandProperty, escapeBidning); cmb.InputBindings.Add(escapeBindingCommand); } } }
One thing that i didn't like was when creating the key binding that you have to specify a tempory command, ApplicationCommands.NotACommand, and then bind over the top of it, if there is any way to avoid that please let me know. Thanks for the origional reply.
From your original post, it sounded like you wanted to add the same exact functionality to several different controls. If that was/is the case, then think about it: Its easier to write ONE attached behavior vs a bunch of MyComboBox, a MyButton, a MyCheckBox, etc controls and C&P the code. The behavior you wrote is hard coded to the ComboBox, but it doesn't have to be hard coded to any specific control type, it can be much more generic. Then you can simply attach it to any control you like. Think of something like a tooltip control. By using attached behaviors, you could attach the tooltip to any control you like. In addition, attached behaviors allow you to modify a controls' behavior without re-templating an entire parent control just to change an embedded control type. I.e. say you had a data grid and wanted to change the behavior on a ComboBox deep down in the visual tree. You'd have to re-template the entire data grid just to change a to a . In regards to your last question, there is no need to create a temporary command and use InputBindings. Just subscribe to the KeyDown message and trap the ESC key.