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

问题描述 投票:0回答:1

我有

<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才会触发命令。如何让它贯穿所有项目?

wpf mvvm mvvm-light
1个回答
1
投票

这很简单。只需将你的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>
© www.soinside.com 2019 - 2024. All rights reserved.