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

时间:2015-02-12 07:09:30

标签: wpf mvvm mvvm-light

我有

<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 仅在我单击文本块时触发命令。如何让它在所有项目中都有效?

1 个答案:

答案 0 :(得分: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>