Wpf列表视图项目选择事件

7

我有两个ListView,每个列表都包含一些行。我想在选择行后调用函数。但是我遇到了一个问题,当选中行或点击该行中的按钮时,事件“GotFocus”会触发。当我使用<i:EventTrigger EventName="Selected">时,表格中的行被选中时它不会触发。我需要怎么做?

Xaml:

<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>

代码:

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");
    }
}
}

什么是“Selected”事件?在哪里可以找到/使用它? - Sinatr
<i:EventTrigger EventName="Selected"> - A191919
1个回答

8

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接