title: How ItemsControl textbox textchange event call?
body: <p>in general <code>TextBox</code> control <code>TextChanged</code> event is working but in <code>ItemsControl</code>, the <code>TextBox</code> <code>TextChanged</code> Event is not fired how can i do this. I trying to do by using following code which I have implemented but not getting result which I want.</p>
  
  <p>So, what am I doing wrong?</p>
  
  <p><strong>View</strong></p>
  
  <pre><code>&lt;Window x:Class="SoniSoft.Window1"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:ff="clr-namespace:SoniSoft"
          Title="Window1" Height="300" Width="300"&gt;
  
      &lt;Window.DataContext&gt;
                  &lt;ff:ViewModels/&gt;
              &lt;/Window.DataContext&gt;
      &lt;Grid&gt;
              &lt;Grid.RowDefinitions&gt;
                  &lt;RowDefinition Height="38"/&gt;
                  &lt;RowDefinition Height="*"/&gt;
              &lt;/Grid.RowDefinitions&gt;
      &lt;TextBox Grid.Row="0" ff:TextBoxBehaviour.TextChangedCommand="{Binding TextChanged}"  /&gt;
  
      &lt;ItemsControl Margin="7,0,0,0" Grid.Row="3" ItemsSource="{Binding Path=ViewModelSearchResults}" x:Name="list"&gt;
                  &lt;ItemsControl.ItemTemplate&gt;
                      &lt;DataTemplate &gt;
                          &lt;Grid&gt;
                              &lt;TextBox  ff:TextBoxBehaviour.TextChangedCommand="{Binding TextChanged}" Text="{Binding Path=CategoryName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" FontSize="14" FontWeight="Normal" x:Name=" TextBoxCategoryName" /&gt;
                          &lt;/Grid&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/ItemsControl.ItemTemplate&gt;
              &lt;/ItemsControl&gt;
          &lt;/Grid&gt;
      &lt;/Window&gt;
  </code></pre>
  
  <p><strong>View Models</strong></p>
  
  <pre><code>class ViewModels :ViewModelBase
              {
                  public ObservableCollection&lt;Category&gt; AllCategorys = new ObservableCollection&lt;Category&gt;();
                  DatabaseDataContext db = new DatabaseDataContext();
                  private ListCollectionView _objViewModelSearchResults;
                  public ListCollectionView ViewModelSearchResults
                  {
                      get { return _objViewModelSearchResults; }
                      set
                      {
                          _objViewModelSearchResults = value;
                          OnPropertyChanged("ViewModelSearchResults");
                      }
                  }
                  public ViewModels()
                  {
                      AllCategorys.Clear();
                      foreach (var item in db.Categories.OrderBy(c =&gt; c.CategoryName))
                      {
                          AllCategorys.Add(item);
                      }
                      ViewModelSearchResults = new ListCollectionView(AllCategorys);
                  }
                  public ICommand TextChanged
                  {
                      get
                      {
                          //  this is very lazy: I should cache the command!
                          return new TextChangedCommand();
                      }
                  }
          private class TextChangedCommand : ICommand
                  {
                      public event EventHandler CanExecuteChanged;
                      public void Execute(object parameter)
                      {
                          MessageBox.Show("Text Changed");
                      }
  
                      public bool CanExecute(object parameter)
                      {
                          return true;
                      }
                  }
              }
  </code></pre>
  
  <p><strong>DependencyProperty</strong></p>
  
  <pre><code>class EventBehaviourFactory
                  {
                      public static DependencyProperty CreateCommandExecutionEventBehaviour(RoutedEvent routedEvent, string propertyName, Type ownerType)
                      {
                          DependencyProperty property = DependencyProperty.RegisterAttached(propertyName, typeof(ICommand), ownerType,
                                                                             new PropertyMetadata(null,
                                                                                 new ExecuteCommandOnRoutedEventBehaviour(routedEvent).PropertyChangedHandler));
  
                          return property;
                      }
      private class ExecuteCommandOnRoutedEventBehaviour : ExecuteCommandBehaviour
                      {
                          private readonly RoutedEvent _routedEvent;
  
                          public ExecuteCommandOnRoutedEventBehaviour(RoutedEvent routedEvent)
                          {
                              _routedEvent = routedEvent;
                          }
  
                          /// &lt;summary&gt;
                          /// Handles attaching or Detaching Event handlers when a Command is assigned or unassigned
                          /// &lt;/summary&gt;
                          /// &lt;param name="sender"&gt;&lt;/param&gt;
                          /// &lt;param name="oldValue"&gt;&lt;/param&gt;
                          /// &lt;param name="newValue"&gt;&lt;/param&gt;
                          protected override void AdjustEventHandlers(DependencyObject sender, object oldValue, object newValue)
                          {
                              UIElement element = sender as UIElement;
                              if (element == null) { return; }
  
                              if (oldValue != null)
                              {
                                  element.RemoveHandler(_routedEvent, new RoutedEventHandler(EventHandler));
                              }
  
                              if (newValue != null)
                              {
                                  element.AddHandler(_routedEvent, new RoutedEventHandler(EventHandler));
                              }
                          }
  
                          protected void EventHandler(object sender, RoutedEventArgs e)
                          {
                              HandleEvent(sender, e);
                          }
                      }
  
                      internal abstract class ExecuteCommandBehaviour
                      {
                          protected DependencyProperty _property;
                          protected abstract void AdjustEventHandlers(DependencyObject sender, object oldValue, object newValue);
  
                          protected void HandleEvent(object sender, EventArgs e)
                          {
                              DependencyObject dp = sender as DependencyObject;
                              if (dp == null)
                              {
                                  return;
                              }
  
                              ICommand command = dp.GetValue(_property) as ICommand;
  
                              if (command == null)
                              {
                                  return;
                              }
  
                              if (command.CanExecute(e))
                              {
                                  command.Execute(e);
                              }
                          }
  
                          /// &lt;summary&gt;
                          /// Listens for a change in the DependencyProperty that we are assigned to, and
                          /// adjusts the EventHandlers accordingly
                          /// &lt;/summary&gt;
                          /// &lt;param name="sender"&gt;&lt;/param&gt;
                          /// &lt;param name="e"&gt;&lt;/param&gt;
                          public void PropertyChangedHandler(DependencyObject sender, DependencyPropertyChangedEventArgs e)
                          {
                              // the first time the property changes,
                              // make a note of which property we are supposed
                              // to be watching
                              if (_property == null)
                              {
                                  _property = e.Property;
                              }
  
                              object oldValue = e.OldValue;
                              object newValue = e.NewValue;
  
                              AdjustEventHandlers(sender, oldValue, newValue);
                          }
                      }
  
                  }
  
               class TextBoxBehaviour
                  {
                      public static readonly DependencyProperty TextChangedCommand = EventBehaviourFactory.CreateCommandExecutionEventBehaviour(TextBox.TextChangedEvent, "TextChangedCommand", typeof(TextBoxBehaviour));
  
                      public static void SetTextChangedCommand(DependencyObject o, ICommand value)
                      {
                          o.SetValue(TextChangedCommand, value);
                      }
  
                      public static ICommand GetTextChangedCommand(DependencyObject o)
                      {
                          return o.GetValue(TextChangedCommand) as ICommand;
                      }
                   }
  </code></pre>