title: WPF-MVVM: Handeling Event from within DataGrid Style
body: <p>I develop Prism+MEF application based on WPF+MVVM, where there are many DataGrids so i build DataGridStyle to be applied for all DataGrids in all modules. The style add Filter Text Box in the Columns Headers, and i used MVVM Light EventToCommand to trigger TextChanged event when the Text Box text is changed like this: (this code exist in DataGridStyle Resource Dictionary)</p>
  
  <pre><code>    &lt;TextBox x:Name="filterTextBox" 
           HorizontalAlignment="Right" MinWidth="25" Height="Auto"
           OpacityMask="Black" Visibility="Collapsed" 
           Text=""
           TextWrapping="Wrap" Grid.Column="0" Grid.ColumnSpan="1"&gt;
  
              &lt;i:Interaction.Triggers&gt;
                  &lt;i:EventTrigger EventName="TextChanged"&gt;
                      &lt;cmd:EventToCommand Command="{Binding DataContext.TextChangedCommand}"
                            PassEventArgsToCommand="True"/&gt;
                  &lt;/i:EventTrigger&gt;
              &lt;/i:Interaction.Triggers&gt;
     &lt;/TextBox&gt;
  </code></pre>
  
  <p>Then i handled the TextChangedCommand in ViewModel (pertaining to View that contains the Data Grid)using Attached property:</p>
  
  <pre><code>#region TextChangedCommand----------------------------------------------------------------------------------------------
  
          static ICommand command; //1
  
          public static ICommand GetTextChangedCommand(DependencyObject obj)
          {
              return (ICommand)obj.GetValue(TextChangedCommandProperty);
          }
  
          public static void SetTextChangedCommand(DependencyObject obj, ICommand value)
          {
              obj.SetValue(TextChangedCommandProperty, value);
          }
  
          // Using a DependencyProperty as the backing store for TextChangedCommand.  This enables animation, styling, binding, etc...
          public static readonly DependencyProperty TextChangedCommandProperty =
              DependencyProperty.RegisterAttached("TextChangedCommand",
                                                   typeof(ICommand), 
                                                   typeof(SubsystemAllViewModel),
                                                   new UIPropertyMetadata(null, CommandChanged));
  
  
          static void CommandChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
          {
              var fe = obj as FrameworkElement;
              command = e.NewValue as ICommand;
              fe.AddHandler(TextBox.TextChangedEvent, new TextChangedEventHandler(ExecuteCommand));
          }
  
          static void ExecuteCommand(object sender, TextChangedEventArgs e)
          {
              //Some  Code
          }
  
  
          #endregion
  </code></pre>
  
  <p>And the View that contains the Grid:</p>
  
  <pre><code> &lt;DataGrid  ItemsSource="{Binding Subsystems,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                     SelectedItem="{Binding Path=SelectedSubsystem, Mode=TwoWay}"                        
                     Name="SubsystemAllDataGrid"              
                     Style="{StaticResource DataGridStyle}"
                     Grid.Row="2"&gt;
  
              &lt;DataGrid.Columns&gt;
                  &lt;DataGridTextColumn Header="Serial" Binding="{Binding Path=Serial, Mode=TwoWay}"&gt;&lt;/DataGridTextColumn&gt;
                  &lt;DataGridTextColumn Header="Type" Binding="{Binding Path=Type, Mode=TwoWay}"&gt;&lt;/DataGridTextColumn&gt;
                  &lt;DataGridTextColumn Header="System" Binding="{Binding Path=System, Mode=TwoWay}"&gt;&lt;/DataGridTextColumn&gt;
                  &lt;DataGridTextColumn Header="SubsystemNo"  Binding="{Binding Path=SubsystemNo, Mode=TwoWay}"&gt;&lt;/DataGridTextColumn&gt;
                  &lt;DataGridTextColumn Header="Description" Binding="{Binding Path=Description, Mode=TwoWay}"&gt;&lt;/DataGridTextColumn&gt;
                  &lt;DataGridTextColumn Header="Scope" Binding="{Binding Path=Scope, Mode=TwoWay}"&gt;&lt;/DataGridTextColumn&gt;
                  &lt;DataGridTextColumn Header="Area" Binding="{Binding Path=Area, Mode=TwoWay}"&gt;&lt;/DataGridTextColumn&gt;
                  &lt;DataGridTextColumn Header="Priority" Binding="{Binding Path=Priority, Mode=TwoWay}"&gt;&lt;/DataGridTextColumn&gt;
                  &lt;DataGridTextColumn Header="DossierLocation" Binding="{Binding Path=DossierLocation, Mode=TwoWay}"&gt;&lt;/DataGridTextColumn&gt;
                  &lt;DataGridTextColumn Header="Parts" Binding="{Binding Path=Parts, Mode=TwoWay}"&gt;&lt;/DataGridTextColumn&gt;
                  &lt;DataGridTextColumn Header="Status" Binding="{Binding Path=Status, Mode=TwoWay}"&gt;&lt;/DataGridTextColumn&gt;
                  &lt;DataGridTextColumn Header="StatusDate" Binding="{Binding Path=StatusDate, Mode=TwoWay}"&gt;&lt;/DataGridTextColumn&gt;
                  &lt;DataGridTextColumn Header="MCDate" Binding="{Binding Path=MCDate, Mode=TwoWay}"&gt;&lt;/DataGridTextColumn&gt;
                  &lt;DataGridTextColumn Header="PlnMCDate" Binding="{Binding Path=PlnMCDate, Mode=TwoWay}"&gt;&lt;/DataGridTextColumn&gt;
                  &lt;DataGridTextColumn Header="Remark" Binding="{Binding Path=Remark, Mode=TwoWay}"&gt;&lt;/DataGridTextColumn&gt;
  
              &lt;/DataGrid.Columns&gt;
  
          &lt;/DataGrid&gt;
  </code></pre>
  
  <p>The problems are:
  When i enter filter text in the text box in one of the columns headers of the data grid nothing happen and break points at following points not hit:</p>
  
  <p>1-GetTextChangedCommand and SetTextChangedCommand </p>
  
  <p>2-The CommandChanged() method.</p>
  
  <p>i'm new to wpf so i'm sure there are faults in WPF or C# code..., so please help me fixing these faults.</p>
  
  <p>Note: i don't use code behind.</p>
  
  <p>Thanks in advance</p>