【问题标题】:Wpf listview item selection eventWpf listview 项目选择事件
【发布时间】:2015-08-19 07:28:52
【问题描述】:

我有两个 ListView,每个列表都包含一些行。我想在行选择后调用函数。但是我有问题,事件“gotfocus”在此行中选择的行或按钮时触发。 When i use <i:EventTrigger EventName="Selected"> it is not firing when row in table is 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 事件?你在哪里找到/使用它?

标签: c# wpf listview


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2015-01-16
    • 1970-01-01
    • 2019-03-17
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多