Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. WPF Code Creation of input Binding

WPF Code Creation of input Binding

Scheduled Pinned Locked Moved WPF
wpfcsharpdatabasewcfhelp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Ed Hill _5_
    wrote on last edited by
    #1

    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.

    S 1 Reply Last reply
    0
    • E Ed Hill _5_

      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.

      S Offline
      S Offline
      SledgeHammer01
      wrote on last edited by
      #2

      Sounds like the perfect use for attached behaviors! :-D

      E 1 Reply Last reply
      0
      • S SledgeHammer01

        Sounds like the perfect use for attached behaviors! :-D

        E Offline
        E Offline
        Ed Hill _5_
        wrote on last edited by
        #3

        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.

        S 1 Reply Last reply
        0
        • E Ed Hill _5_

          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.

          S Offline
          S Offline
          SledgeHammer01
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups