c# - 列表框选择更改事件 : get the value before it was changed

标签 c# wpf xaml

我正在开发一个 C# wpf 应用程序,其中有一个列表框,我想获取在发生更改之前选择的元素的值

我通过这种方式成功获得了新的值(value):

<ListBox SelectionChanged="listBox1_SelectedIndexChanged"... />

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        test.add(listBox1.SelectedItem.ToString());
    }

但我需要像 listBox1.UnselectedItem 这样的东西来获取在更改期间未选择的元素。有什么想法吗?

最佳答案

SelectionChangedEventArgs 有一个名为 RemovedItems 的属性,其中包含随新选择一起删除的项目列表。您可以将 EventArgs 替换为 SelectionChangedEventArgs 并访问参数的属性(转换也可以,因为它是一个子类)。

    private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        List<string> oldItemNames = new List<string>();
        foreach(var item in e.RemovedItems)
        {
            oldItemNames.Add(item.ToString());
        }
    }

关于c# - 列表框选择更改事件 : get the value before it was changed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31520976/

相关文章:

c# - 如何使用 MySQL 配置流畅的 nHibernate

wpf - 重构 DataTemplate (XAML) 以减少重复

c# - wpf 窗口刷新首先起作用,然后停止

c# - WPF Datagrid 多项选择在拖动时丢失

c# - boolean 值作为方法参数不改变状态

c# - 是否需要一直打开一个excel文件,同时导入数据到数据表,C#

android - 在 Xamarin.Forms 中布局背景图像

c# - 在 XAML 中使用 RadioButtons 启用/禁用 WPF 控件

c# - 广告在 WP 应用程序中不起作用

c# - NumberFormatInfo 不删除小数