尝试响应简单ListView项目单击

时间:2017-07-11 20:08:10

标签: c# wpf listview itemcontainerstyle

简单的目标。点击%$ ^%#clown ...获取*& ^ $ ^ $消息框。我希望能够对列表视图的单行上的点击作出反应。事实证明这很困难。我知道正在应用ItemContainerStyle ......这些项目都被推到了右边。那么为什么事件没有解雇?

这是XAML:

    <Window x:Class="TryListView.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TryListView"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525"
        Background="Black">
    <Grid Background="Transparent">
        <ListView Grid.Row="1" x:Name="lbClowns" Background="Transparent">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Border Margin="5" BorderBrush="White" BorderThickness="1">
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>
                                <TextBlock Margin="5,0,0,0" Grid.Row="0" x:Name="NameTxt" TextWrapping="Wrap" Text="{Binding ClownName}" FontSize="28" Foreground="White"/>
                                <TextBlock Margin="5,0,0,0" Grid.Row="1" x:Name="TrickTxt"  TextWrapping="Wrap" Foreground="White" FontSize="18" Text="{Binding ClownTrick}" />
                            </Grid>
                        </Border>
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="HorizontalContentAlignment" Value="Right" />
                    <EventSetter Event="MouseLeftButtonDown" Handler="KlownKlicked" />
                </Style>
            </ListView.ItemContainerStyle>
        </ListView>

    </Grid>
</Window>

这是代码隐藏的代码:

using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;

namespace TryListView
{
    public class Clown
    {
        public string ClownName { get; set; }
        public string ClownTrick { get; set; }
    }

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            PopulateMyListView();
        }

        private void PopulateMyListView()
        {
            var ListOClownz = new List<Clown>()
            {
                new Clown() { ClownName = "Krusty", ClownTrick="Juggling" },
                new Clown() { ClownName = "Pennywise", ClownTrick="Human Sacrifice" },
                new Clown() { ClownName = "Shakes", ClownTrick="Vomiting" },
                new Clown() { ClownName = "Bozo", ClownTrick="Swordfighting" },
            };

            lbClowns.ItemsSource = ListOClownz;
        }

        private void KlownKlicked(object sender, MouseButtonEventArgs e)
        {
            MessageBox.Show("Congratulations, you klicked a klown. Now get lost.");
        }
    }
}

Screenshot

1 个答案:

答案 0 :(得分:0)

MouseButtonEventArgs在整个过程中被标记为Handled(e.Handled)(很可能是由于选择了ListViewItem)。

如果使用PreviewMouseLeftButtonDown事件

,您可以触发点击方法
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="KlownKlicked" />
private void KlownKlicked(object sender, MouseButtonEventArgs e)
{
    var clown = ((ListViewItem)sender).DataContext as Clown;
    MessageBox.Show(String.Format("Congratulations, you klicked a klown {0}.", clown.ClownName));
}

...作为替代方案,请尝试使用ListView.SelectionChanged事件:

<ListView Grid.Row="1" x:Name="lbClowns" 
          Background="Transparent" 
          SelectionChanged="ListSelectionChanged">
private void ListSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var clown = (Clown)lbClowns.SelectedItem;
    MessageBox.Show(String.Format("Congratulations, you klicked a klown {0}.", clown.ClownName));
}
相关问题