0

I've been using i:Interaction.Triggers and an EventToCommand implementation to handle certain control events in my VM.

<DataGrid ...>
<i:Interaction.Triggers>
    <i:EventTrigger EventName="AutoGeneratingColumn">
        <ui:EventToCommand Command="{Binding Path=DataContext.AutoGeneratingColumnCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"
                            PassEventArgsToCommand="True" />

All worked fine. Then I needed to have multiple DataGrids, so I created a ItemsControl template that has the DataGrid with the interaction triggers. I noticed in this case, the triggers are not working.

Is this a known issue? I debugged the EventToCommand class and it is calling the Attach member, but it never calls the Invoke.

The ItemsControl looks like:

<ItemsControl>
   <ItemsControl.ItemsTemplate>
<DataGrid ...>
<i:Interaction.Triggers>
    <i:EventTrigger EventName="AutoGeneratingColumn">
        <ui:EventToCommand Command="{Binding Path=DataContext.AutoGeneratingColumnCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"
                            PassEventArgsToCommand="True" />

Any ideas? Am I doing something wrong?

EDIT:

<ItemsControl Grid.Row="2" ItemsSource="{Binding StoredProcedureResults}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <DataGrid EnableColumnVirtualization="True" EnableRowVirtualization="True" SelectionMode="Extended" SelectionUnit="CellOrRowHeader"
                        CanUserAddRows="False" CanUserDeleteRows="False" IsReadOnly="True" BorderThickness="0,0,0,0" ItemsSource="{Binding}">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="AutoGeneratingColumn">
                        <UI:EventToCommand Command="{Binding Path=DataContext.AutoGeneratingColumnCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"
                                            PassEventArgsToCommand="True" />
                    </i:EventTrigger>
                    <i:EventTrigger EventName="LoadingRow">
                        <UI:EventToCommand Command="{Binding Path=DataContext.LoadingRowCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"
                                            PassEventArgsToCommand="True" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </DataGrid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
7
  • An ItemsControl template that has the DataGrid? Nov 10, 2017 at 19:53
  • @EdPlunkett Yes, I have updated the question. Works fine as a standalone DataGrid, but not inside of an ItemsControl. Nov 10, 2017 at 19:58
  • Can I see the XAML, please? Enough to test what you're actually doing. This seems like a much better case for a UserControl than for whatever this is. Nov 10, 2017 at 20:00
  • @EdPlunkett I have added a more complete portion... the EventToCommand is part of my MVVM framework and the 2 commands are just RelayCommandT implementations. It worked as a standalone DataGrid, not sure why it doesn't inside of a ItemsControl. Nov 10, 2017 at 20:07
  • @EdPlunkett Gonna get this to work with an attached property / behavior I guess unless you find the issue :), but I'm thinking its probably one of those random WPF flukes that don't work as they should :). Nov 10, 2017 at 20:08

1 Answer 1

1

This is a combination of an issue with System.Windows.Interactivity EventTrigger and some circumstances.

Interactivity.EventTrigger can't handle an event fired before FrameworkElement.Loaded event

It internally uses associated object's Loaded event to attach event handlers for all events except the Loaded event itself. OnAttached method from your EventToCommand is called from TriggerAction.Attach, which actually attaches TriggerAction to the associated object (DataGrid here). AssociatedObject property on TriggerAction is not a DependencyProperty, and as TriggerAction inherits from Freezable, this method does freezable related stuff (calls WritePreamble and WritePostscript). The actual event handlers have not been attached yet.

DataGrid AutoGeneratingColumn and LoadingRow

If ItemsSource can be determined when DataGrid is initialized (DataGrid can figure out which columns and rows it needs to generate), these events fire before the Loaded event. I assume that when your DataGrid was not part of DataTemplate, it's ItemsSource was bound to a property on VM whose value was null at the time DataGrid is initialized. Once you moved your DataGrid to DataTemplate, when you set ItemsSource property of your ItemsControl and DataGrids are initialized for each ItemControl's Item, DataGrid's ItemsSource can be determined during initialization and these events fire before the Loaded event. If you allowed users to add rows to your DataGrid (IsReadOnly to false, and CanUserAddRows to true), your LoadingRowCommand would be executed after DataGrid.IsLoaded becomes true and user adds a row.

To get this to work you would have to not set DataGrid's ItemsSource within DataTemplate (basically remove ItemsSource={Binding}), wait for the Loaded event and then set it. I'd say that goes against the idea of item template (and is there a nice way to do this?), so I would go for attached behaviors for these two events as you suggested in your comment.

1
  • Great explanation.. thanks! In answer to your question, yes, the standalone DataGrid.ItemsSource was bound to StoredProcedureResults (as the ItemsControl is now). That value was null until the user clicked a button to execute the SP. When I moved to the ItemsControl to handle multiple result sets, ItemsControl.ItemsSource was pointed to the array of DataTables which would have values and such and jive with your explanation. Nov 13, 2017 at 0:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.