整个列表视图项目的事件触发器

Event trigger for entire listview item

我有

<ListView ScrollViewer.VerticalScrollBarVisibility="Auto" Grid.Row="1" Grid.Column="0" Margin="2" Name="CoursesListView" ItemsSource="{Binding CourseTags}">
            <ListView.ItemContainerStyle>
                <Style TargetType="{x:Type ListViewItem}">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Border Margin="2" BorderThickness="1" BorderBrush="Red" Background="AntiqueWhite" CornerRadius="2" Cursor="Hand">
                        <WrapPanel ToolTip="{Binding Description}" HorizontalAlignment="Stretch">
                            <TextBlock Text="{Binding Name}" FontWeight="Bold" />
                            <TextBlock Text=" (" />
                            <TextBlock Text="{Binding NumberOfCourses}" TextDecorations="Underline" Foreground="Blue"  />
                            <TextBlock Text=")" />
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="MouseLeftButtonUp">
                                    <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding DataContext.ProductTagSelectedCommand, ElementName=LayoutRoot}"
                                        CommandParameter="{Binding Name}" />
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                    </WrapPanel>
                    </Border>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

这些项目拉伸得很好,并且无论它们有多少文本都具有相同的宽度。手形光标也正确显示(无论我指向哪里)问题是 EventTrigger 仅当我单击文本块时才触发命令。如何让它贯穿所有项目?

很简单。只需将 EventTrigger 放在 Border 中,而不是 WrapPanel:

<Border Margin="2" BorderThickness="1" BorderBrush="Red" Background="AntiqueWhite" CornerRadius="2" Cursor="Hand">
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="MouseLeftButtonUp">
      <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding DataContext.ProductTagSelectedCommand, ElementName=LayoutRoot}" CommandParameter="{Binding Name}" />
    </i:EventTrigger>
  </i:Interaction.Triggers>
  <WrapPanel ToolTip="{Binding Description}" HorizontalAlignment="Stretch">
    <TextBlock Text="{Binding Name}" FontWeight="Bold" />
    <TextBlock Text=" (" />
    <TextBlock Text="{Binding NumberOfCourses}" TextDecorations="Underline" Foreground="Blue"  />
    <TextBlock Text=")" />
  </WrapPanel>
</Border>