Prism-MEF-MVVM Light: using EventToCommand to handle XAML events

Topics: Prism v4 - WPF 4
Apr 22, 2013 at 8:34 PM
Edited Apr 22, 2013 at 8:40 PM
Hi Prism Experts,
I have TextBox resides in DataGridColumnHeader in style Resource Dictionary, the TextBox used as a Filter so i need to trigger TextChanged event and handle it in View Model to implement filteration process.The point is i need to commit to followinh requirements:
1.I use Prism+MEF
2.No Code Behind
3.Event handeling in View Model
4.USe style to be able to apply it for all DataGrids consistently
5.Pass the Event arguments to the View Model to use in filteration process

So i tried to use MVVM Light: EventToCommand in the DataGrideStyle ResourceDictionary
like this

    <local:HeaderFilterConverter x:Key="headerConverter"/>

    <Style x:Key="ColumnHeaderTextBoxStyle" TargetType="{x:Type TextBox}">
        <Setter Property="Template">
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <Border x:Name="Bd" SnapsToDevicePixels="true" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
                        <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
                                <i:EventTrigger EventName="TextChanged">

                                    <cmd:EventToCommand  Command="{Binding local:TextChangedBehavior.TextChangedCommand, ElementName=filterTextBox}"
                                                         CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TextBox}}, Path=Name}"/>
__                        </ScrollViewer>

    <!-- -->
    <Style TargetType="{x:Type DataGridColumnHeader}">
        <!--xaml code removed to cut it short-->    
                            <ContentPresenter x:Name="contentPresenter"
                                    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                    VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
                                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                    ContentStringFormat="{TemplateBinding ContentStringFormat}" 
                                    ContentTemplate="{TemplateBinding ContentTemplate}">
                                    <MultiBinding Converter="{StaticResource headerConverter}">
                                            <Binding ElementName="filterTextBox" Path="Text" />
                                            <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Content" />
                            __<!--Filter Text Box-->
                            <TextBox x:Name="filterTextBox" HorizontalAlignment="Right" 
                                         MinWidth="25" Height="Auto" OpacityMask="Black" Visibility="Collapsed" 
                                         Text="{Binding Path=DataContext.FilterText, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}} ,Mode=TwoWay,UpdateSourceTrigger=Explicit}"                                                                              
                                         Style="{StaticResource ColumnHeaderTextBoxStyle}"
                                         TextWrapping="Wrap" Grid.Column="0" Grid.ColumnSpan="1">                                
                        <Trigger Property="IsMouseOver" Value="True">
                      <!--rest of xaml code-->
And here is my Attached Behavior class:

public static class TextChangedBehavior
        #region TextChangedCommand-----------------------------------------------------------

        static ICommand command; //1

        public static ICommand GetTextChangedCommand(TextBox target)

            return (ICommand)target.GetValue(TextChangedCommandProperty);


        public static void SetTextChangedCommand(TextBox target, ICommand value)

            target.SetValue(TextChangedCommandProperty, value);


        public static readonly DependencyProperty TextChangedCommandProperty =
                                                 new PropertyMetadata(TextChangedCommandChanged));
                                                 //new UIPropertyMetadata(new PropertyChangedCallback(TextChangedCommandChanged)));

        static void TextChangedCommandChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)

            TextBox element = target as TextBox;

            if (element != null)
                if (e.NewValue != null)
                    element.TextChanged += Element_TextChanged;
                    element.TextChanged -= Element_TextChanged;


        static void Element_TextChanged(object sender, TextChangedEventArgs e)

            // Get the textbox
            //TextBox filterTextBox = e.OriginalSource as TextBox;

            // Get the header of the textbox
            //DataGridColumnHeader header = TryFindParent<DataGridColumnHeader>(filterTextBox);
            //if (header != null)
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 SetTextChangedCommand

2-The TextChangedChanged() method. (not Called)

3- The Attached Property statement

i don't know if i have errors in the code or the problem between MVVM Light and Prism+MEF
So i need your help fix this.

Thanks in advance
Apr 24, 2013 at 7:31 AM
Help [:(]
Apr 24, 2013 at 8:56 PM
Hi Jivara,

In the last few days we tried to implement the functionality you are describing using the code snippets you posted but without any success...
However, we tried to implement something similar from scratch and we were able to set an style to a TextBox control that could be reused in different views to execute a Command in the view model of the view when the text is changed:
<Style TargetType="{x:Type TextBox}">
    <Setter Property="Template">
            <ControlTemplate TargetType="{x:Type TextBox}">
                        <i:EventTrigger EventName="TextChanged">

                            <cmd:EventToCommand Command="{Binding Mode=OneWay, Path=TextChangedCommand}" PassEventArgsToCommand="True"/>


Where TextChangedCommand is simply a command in the view model of the view containing the TextBox. I hope this helps as a starting point to implement the feature you are describing.

Also, as topic doesn't seem to be related to Prism but to WPF, I believe you might find better support regarding how to implement such features in the following forums:

Damian Cherubini
Apr 25, 2013 at 3:47 PM
Hi Damian,
I'm grateful for tying to help, knowing that i already sent same question to forums you mentioned but w/o response. So i'm gratefull for your commitment to help us .
I already started another approach similar to the one you advised beacuse the Attached Behavior is not working but the point now is: how to pass the event sender and eventArgs to the command in the view model.
Sorry if i'm asking agin but truly i didn't find response from the other froums.
Apr 25, 2013 at 8:27 PM

I am not sure it this is what you need, but I believe you can obtain the control who fired the event trough the Source property of the TextChangedEventArgs received as the parameter of the command (thanks to the PassEventArgsToCommand property).

I hope this helps,

Damian Cherubini