WPF-MVVM: Handeling Event from within DataGrid Style

Apr 18, 2013 at 7:27 PM
Hi,
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)
<TextBox x:Name="filterTextBox" 
         HorizontalAlignment="Right" MinWidth="25" Height="Auto"
         OpacityMask="Black" Visibility="Collapsed" 
         Text=""
         TextWrapping="Wrap" Grid.Column="0" Grid.ColumnSpan="1">

            <i:Interaction.Triggers>
                <i:EventTrigger EventName="TextChanged">
                    <cmd:EventToCommand Command="{Binding DataContext.TextChangedCommand}"
                          PassEventArgsToCommand="True"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
   </TextBox>

Then i handled the TextChangedCommand in ViewModel (pertaining to View that contains the Data Grid)using Attached property:

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
And the View that contains the Grid:
<DataGrid  ItemsSource="{Binding Subsystems,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                   SelectedItem="{Binding Path=SelectedSubsystem, Mode=TwoWay}"                        
                   Name="SubsystemAllDataGrid"              
                   Style="{StaticResource DataGridStyle}"
                   Grid.Row="2">

            <DataGrid.Columns>
                <DataGridTextColumn Header="Serial" Binding="{Binding Path=Serial, Mode=TwoWay}"></DataGridTextColumn>
                <DataGridTextColumn Header="Type" Binding="{Binding Path=Type, Mode=TwoWay}"></DataGridTextColumn>
                <DataGridTextColumn Header="System" Binding="{Binding Path=System, Mode=TwoWay}"></DataGridTextColumn>
                <DataGridTextColumn Header="SubsystemNo"  Binding="{Binding Path=SubsystemNo, Mode=TwoWay}"></DataGridTextColumn>
                <DataGridTextColumn Header="Description" Binding="{Binding Path=Description, Mode=TwoWay}"></DataGridTextColumn>
                <DataGridTextColumn Header="Scope" Binding="{Binding Path=Scope, Mode=TwoWay}"></DataGridTextColumn>
                <DataGridTextColumn Header="Area" Binding="{Binding Path=Area, Mode=TwoWay}"></DataGridTextColumn>
                <DataGridTextColumn Header="Priority" Binding="{Binding Path=Priority, Mode=TwoWay}"></DataGridTextColumn>
                <DataGridTextColumn Header="DossierLocation" Binding="{Binding Path=DossierLocation, Mode=TwoWay}"></DataGridTextColumn>
                <DataGridTextColumn Header="Parts" Binding="{Binding Path=Parts, Mode=TwoWay}"></DataGridTextColumn>
                <DataGridTextColumn Header="Status" Binding="{Binding Path=Status, Mode=TwoWay}"></DataGridTextColumn>
                <DataGridTextColumn Header="StatusDate" Binding="{Binding Path=StatusDate, Mode=TwoWay}"></DataGridTextColumn>
                <DataGridTextColumn Header="MCDate" Binding="{Binding Path=MCDate, Mode=TwoWay}"></DataGridTextColumn>
                <DataGridTextColumn Header="PlnMCDate" Binding="{Binding Path=PlnMCDate, Mode=TwoWay}"></DataGridTextColumn>
                <DataGridTextColumn Header="Remark" Binding="{Binding Path=Remark, Mode=TwoWay}"></DataGridTextColumn>

            </DataGrid.Columns>

        </DataGrid>
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:

1-GetTextChangedCommand and SetTextChangedCommand

2-The CommandChanged() method.

i'm new to wpf so i'm sure there are faults in WPF or C# code..., so please help me fixing these faults.

Note: i don't use code behind.

Thanks in advance
Apr 19, 2013 at 8:40 PM
No ideas?!!!
Apr 24, 2013 at 7:38 AM
HELP [:((]