title: Hooking a command to a CustomDataGridColumn using ICommandSource
body: <p>I am very new to custom controls forgive me for that, but i am trying to Hook a Command to the <code>SelectionChangedEvent</code> of a <code>DataGridComboBoxColumn</code>. It seams that everything is working except my command is null. I am having some trouble why it is still null. Can you please tell me why I am still null and why?</p>
  
  <pre><code>   public class CustomComboBoxColumn : DataGridComboBoxColumn, ICommandSource
  {
      private bool _canExecute;
      static CustomComboBoxColumn()
      {
      }
  
      private EventHandler canExecuteChagnedEventHandler;
  
      #region Dependancy Properties
  
      /// &lt;summary&gt;
      /// Allows the combobox to be editable
      /// &lt;/summary&gt;
      public static readonly DependencyProperty IsEditableProperty = DependencyProperty.Register(
          "IsEditable", typeof(bool), typeof(CustomComboBoxColumn), new PropertyMetadata(default(bool)));
      public bool IsEditable
      {
          get { return (bool)GetValue(IsEditableProperty); }
          set { SetValue(IsEditableProperty, value); }
      }
  
  
      /// &lt;summary&gt;
      /// Command Property
      /// &lt;/summary&gt;
      public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
          "Command", typeof(ICommand), typeof(CustomComboBoxColumn),
          new PropertyMetadata((ICommand)null, new PropertyChangedCallback(OnCommandChanged)));
  
      [TypeConverter(typeof(CommandConverter))]
      public ICommand Command
      {
          get { return (ICommand)GetValue(CommandProperty); }
          set { SetValue(CommandProperty, value); }
      }
  
  
      /// &lt;summary&gt;
      /// Command Parameter
      /// &lt;/summary&gt;
      public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register(
          "CommandParameter", typeof(object), typeof(CustomComboBoxColumn), new PropertyMetadata(default(object)));
  
      public object CommandParameter
      {
          get { return (object)GetValue(CommandParameterProperty); }
          set { SetValue(CommandParameterProperty, value); }
      }
  
  
      /// &lt;summary&gt;
      /// Command Target
      /// &lt;/summary&gt;
      public static readonly DependencyProperty CommandTargetProperty = DependencyProperty.Register(
          "CommandTarget", typeof(IInputElement), typeof(CustomComboBoxColumn),
          new PropertyMetadata(default(IInputElement)));
  
      public IInputElement CommandTarget
      {
          get { return (IInputElement)GetValue(CommandTargetProperty); }
          set { SetValue(CommandTargetProperty, value); }
      }
  
      #endregion
  
      #region Properties
  
      private ComboBox _comboBox;
  
      public ComboBox ComboBox
      {
          get { return _comboBox; }
          set
          {
              if (_comboBox != null)
                  _comboBox.SelectionChanged -= OnSelectionChanged;
              _comboBox = value;
              if (_comboBox != null)
                  _comboBox.SelectionChanged += OnSelectionChanged;
          }
      }
  
      #endregion
  
      protected override object PrepareCellForEdit(FrameworkElement editingElement, RoutedEventArgs editingEventArgs)
      {
          var comboBox = editingElement as ComboBox;
          if (comboBox != null)
          {
              ComboBox = comboBox;
              comboBox.IsEditable = IsEditable;
          }
          return base.PrepareCellForEdit(editingElement, editingEventArgs);
      }
  
  
      private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
      {
          FireCommand();
      }
  
      private void FireCommand()
      {
          if (Command != null)
          {
              Command.Execute(CommandParameter);
          }
      }
  
      /// &lt;summary&gt;
      /// Calls OnCommandChanged to hook up new commands
      /// &lt;/summary&gt;
      /// &lt;param name="d"&gt;&lt;/param&gt;
      /// &lt;param name="e"&gt;&lt;/param&gt;
      private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
      {
          CustomComboBoxColumn control = d as CustomComboBoxColumn;
          if (control != null)
              control.OnCommandChanged((ICommand)e.OldValue, (ICommand)e.NewValue);
      }
      /// &lt;summary&gt;
      /// Calls UnhookCommand and HookCommand
      /// &lt;/summary&gt;
      /// &lt;param name="oldCommand"&gt;&lt;/param&gt;
      /// &lt;param name="newCommand"&gt;&lt;/param&gt;
      protected virtual void OnCommandChanged(ICommand oldCommand, ICommand newCommand)
      {
          if (oldCommand != null)
              UnHookCommand(oldCommand, newCommand);
          HookCommand(oldCommand, newCommand);
      }
      /// &lt;summary&gt;
      /// Attaches CanExecuteChanged EventHandler
      /// &lt;/summary&gt;
      /// &lt;param name="oldCommand"&gt;&lt;/param&gt;
      /// &lt;param name="newCommand"&gt;&lt;/param&gt;
      private void HookCommand(ICommand oldCommand, ICommand newCommand)
      {
          EventHandler handler = CanExecuteChagned;
          canExecuteChagnedEventHandler = handler;
          if (newCommand != null)
              newCommand.CanExecuteChanged += canExecuteChagnedEventHandler;
      }
      /// &lt;summary&gt;
      /// Detaches CanExecuteChagned EventHandler
      /// &lt;/summary&gt;
      /// &lt;param name="oldCommand"&gt;&lt;/param&gt;
      /// &lt;param name="newCommand"&gt;&lt;/param&gt;
      private void UnHookCommand(ICommand oldCommand, ICommand newCommand)
      {
          EventHandler handler = CanExecuteChagned;
          oldCommand.CanExecuteChanged -= CanExecuteChagned;
      }
  
  
      /// &lt;summary&gt;
      /// Sets CanExecute
      /// &lt;/summary&gt;
      /// &lt;param name="sender"&gt;&lt;/param&gt;
      /// &lt;param name="e"&gt;&lt;/param&gt;
      private void CanExecuteChagned(object sender, EventArgs e)
      {
          if (Command != null)
          {
              RoutedCommand rc = Command as RoutedCommand;
              if (rc != null)
              {
                  _canExecute = rc.CanExecute(CommandParameter, CommandTarget) ? true : false;
              }
              else
              {
                  _canExecute = Command.CanExecute(CommandParameter) ? true : false;
              }
          }
      }
  }
  </code></pre>
  
  <p>ViewModel:</p>
  
  <pre><code>public ViewModel()
  {
  ChangeDescriptionCommand = new DelegateCommand(UpdateDescription);
  }
  public DelegateCommand ChangeDescriptionCommand { get; set; }
  //..other methods
  </code></pre>
  
  <p>xaml:</p>
  
  <pre><code>&lt;icc:CustomComboBoxColumn Header="Item Number"
                                            DisplayMemberPath="Code"
                                            SelectedValuePath="Id"
                                            x:Name="ItemColumn"
                                            SelectedValueBinding="{Binding PartId}"
                                            Command="{Binding ChangeDescriptionCommand}"&gt;
                      &lt;icc:CustomComboBoxColumn.ElementStyle&gt;
                                  &lt;Style TargetType="{x:Type ComboBox}"&gt;
                                      &lt;Setter Property="ItemsSource" Value="{Binding Path=DataContext.Parts,
                                          RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"/&gt;
                                  &lt;/Style&gt;
                              &lt;/icc:CustomComboBoxColumn.ElementStyle&gt;
                              &lt;icc:CustomComboBoxColumn.EditingElementStyle&gt;
                                  &lt;Style TargetType="{x:Type ComboBox}"&gt;
                                      &lt;Setter Property="ItemsSource" Value="{Binding Path=DataContext.Parts,
                                          RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"/&gt;
                                  &lt;/Style&gt;
                              &lt;/icc:CustomComboBoxColumn.EditingElementStyle&gt;
                          &lt;/icc:CustomComboBoxColumn&gt;
  </code></pre>
  
  <p><strong>Update</strong>
  So digging into this much more I found that for some reason the command is not being initialized. </p>
  
  <p><strong>Update2</strong>
  I have created a totally new control referencing <a href="https://msdn.microsoft.com/en-us/library/ms748978(v=vs.110).aspx" rel="nofollow">How To Implament ICommandSource</a> went step by step and it the slider control from the link worked. I copy pasted into new class changed <code>Slider</code> to <code>DataGridComboBoxColumn</code> and same issue I was having in my control. null command.</p>