wpf listview 项目选择事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32089097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:20:55  来源:igfitidea点击:

Wpf listview item selection event

c#wpflistview

提问by A191919

I have two ListView, each list contains some row's. I want to call function after row selection. But i have a problem, event "GotFocus" firing when row selected or button in this row clicked. When i use <i:EventTrigger EventName="Selected">it is not firing when row in table is selected. What i need to do?

我有两个 ListView,每个列表包含一些行。我想在行选择后调用函数。But i have a problem, event "GotFocus" firing when row selected or button in this row clicked. 当我<i:EventTrigger EventName="Selected">选择表中的行时,它不会触发。我需要做什么?

Xaml:

Xml:

<Grid>
    <ListView Width="200" Height="200" ItemsSource="{Binding Items}" HorizontalAlignment="Left">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Button Content="{Binding .}">
                </Button>
            </DataTemplate>
        </ListView.ItemTemplate>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="GotFocus">
                <i:InvokeCommandAction Command="{Binding DataContext.TestCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type vm:MainWindow }}}"></i:InvokeCommandAction>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </ListView>
    <ListBox Width="200" Height="200" ItemsSource="{Binding Items}" HorizontalAlignment="Right">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Button Content="{Binding .}">
                </Button>
            </DataTemplate>
        </ListBox.ItemTemplate>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="GotFocus">
                <i:InvokeCommandAction Command="{Binding DataContext.TestTestCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type vm:MainWindow }}}"></i:InvokeCommandAction>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </ListBox>
</Grid>

Code:

代码:

namespace WpfApplication129
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{

    public MainWindow()
    {
        DataContext = new Data();
        InitializeComponent();
    }
}
public class Data
{
    public ICommand TestCommand { get; set; }
    public ICommand TestTestCommand { get; set; }
    public List<string> Items { get; set; }
    public Data()
    {
        TestCommand = new RelayCommand(() => Test());
        TestTestCommand = new RelayCommand(() => TestTest());
        Items = new List<string>();
        Items.Add("first");
        Items.Add("Second");
        Items.Add("Third");
    }
    public void Test()
    {
        MessageBox.Show("Running");
    }
    public void TestTest()
    {
        MessageBox.Show("TestRunning");
    }
}
}

采纳答案by Sinatr

There is no Selectedevent in ListView, you have to use SelectionChangedevent.

中没有Selected事件ListView,您必须使用SelectionChanged事件。