2

I have the following ItemsControl which handles the display of items added to it:

 <ControlTemplate x:Key="MyItemsControlTemplate">
    <ItemsControl  x:Name="MyItemsControl" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type mynameSpace:MyClass}}, Path=ItemsSource}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel IsItemsHost="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                    <WrapPanel.Resources>                                    
                        <HierarchicalDataTemplate DataType="{x:Type local:MyCustomClass}">
                            <Button Command="{Binding}">
                                <TextBlock  Text="{Binding Path=DisplayValue}"/>
                            </Button>
                        </HierarchicalDataTemplate>                                 
                    </WrapPanel.Resources>
                </WrapPanel>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
</ControlTemplate>

You can see that the Button has a Command="{Binding}" which handles the click event and calls a command.

Now I want to also use a ListView to display the same items and handle the click event. I see from this link WPF: How to bind a command to the ListBoxItem using MVVM? that I have to use Interaction.Triggers so I have did the following:

<ControlTemplate x:Key="MyListViewControlTemplate">
    <ListView Name="MyListView" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type mynameSpace:MyClass}}, Path=ItemsSource}">

        <ListView.View>
            <GridView >
                <GridViewColumn Header="Details" DisplayMemberBinding="{Binding Path=DisplayValue}"/>
            </GridView>
        </ListView.View>

        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseLeftButtonUp">
                <i:InvokeCommandAction Command="{Binding}"></i:InvokeCommandAction>
            </i:EventTrigger>
        </i:Interaction.Triggers>

    </ListView>
</ControlTemplate>

The items display properly in the ListView so I know the binding is correct for the text display but I find that the click handler for the ListView does not fire the command like the Button object does.
Any ideas?

How

Community
  • 1
  • 1
Harry Boy
  • 4,159
  • 17
  • 71
  • 122

1 Answers1

2

You should apply the interaction trigger to an element within the ListView. You could use a CellTemplate with a TextBlock that you apply the interaction trigger to:

<ControlTemplate x:Key="MyListViewControlTemplate">
    <ListView Name="MyListView" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type mynameSpace:MyClass}}, Path=ItemsSource}">
        <ListView.View>
            <GridView >
                <GridViewColumn Header="Details">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding DisplayValue}">
                                        <i:Interaction.Triggers>
                                            <i:EventTrigger EventName="MouseLeftButtonUp">
                                                <i:InvokeCommandAction Command="{Binding}"></i:InvokeCommandAction>
                                            </i:EventTrigger>
                                        </i:Interaction.Triggers>
                            </TextBlock>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
</ControlTemplate>
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Thank you this has fixed my problem. I next want to have 2 Columns in my datagrid so I have used your method and added two inner TextBlocks with Interaction.Triggers. Is there a way I can wrap the whole item in a single trigger? I find that if the I click on the row to the right of a textbox the event does not fire. – Harry Boy Mar 22 '17 at 11:44
  • Unfortunately you cannot use an interaction trigger in a style. An alternative may be to use an attached behaviour that invokes the command: https://www.codeproject.com/Articles/28959/Introduction-to-Attached-Behaviors-in-WPF – mm8 Mar 22 '17 at 12:29