c# - 查找选定的 WPF 列表框条目

标签 c# wpf .net-3.5

我一直在尝试使用以下代码遍历 WPF 中列表框的选定项;

        try
        {
            for (int i = 0; i < mylistbox.SelectedItems.Count; i++)
            {
                ListItem li = (ListItem)mylistbox.SelectedItems[i];

                string listitemcontents_str = li.ToString();
            }
        }
        catch(Exception e)
        {
            // Error appears here
            string error = e.ToString();
        }

但是我收到一个无效的转换异常;

System.InvalidCastException:无法将“mylist”类型的对象转换为类型“System.Windows.Documents.ListItem”。

有解决办法吗?

最佳答案

我更喜欢使用数据绑定(bind)来做到这一点: Sync SelectedItems in a muliselect listbox with a collection in ViewModel

关于c# - 查找选定的 WPF 列表框条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2781336/

相关文章:

c# - 在 WCF 中返回许多接口(interface)

c# - 无法实例化连接提供程序: NHibernate. Driver.MySqlDataDriver

c# - 请告诉我问题是什么 c# regex.split()

c# - 翻转瓷砖游戏

c# - 绑定(bind)到两个值

.net - WPF 中现有控件的 ControlTemplate

c# - 将对象集合连接成逗号分隔的字符串

环绕面板中用户控件的 WPF MVVM 动态集合

c# - 从输出参数中提取类

c# - 如何从强制终止管道中正确清除异步回调?