Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用ListBox.Items作为列表的Itemsource<;字符串>;_C#_Wpf_List_Listbox - Fatal编程技术网

C# 如何使用ListBox.Items作为列表的Itemsource<;字符串>;

C# 如何使用ListBox.Items作为列表的Itemsource<;字符串>;,c#,wpf,list,listbox,C#,Wpf,List,Listbox,我有一个列表框。我在代码隐藏中也有一个列表。我想从列表框中获取所有项目,作为我的列表的源 我试着这样做: 列表提要=新列表(); //获取来源 var feed=MyList.Items.Cast().ToList(); 但它抛出了一个异常:System.InvalidCastException 怎么办?列表提要=新列表(); list<T> feed = new list<T>(); var feed = MyList.Items.Cast<T>().T

我有一个
列表框
。我在代码隐藏中也有一个
列表
。我想从
列表框
中获取所有项目,作为我的
列表
的源

我试着这样做:

列表提要=新列表();
//获取来源
var feed=MyList.Items.Cast().ToList();
但它抛出了一个异常:
System.InvalidCastException

怎么办?

列表提要=新列表();
list<T> feed = new list<T>();
var feed = MyList.Items.Cast<T>().ToList();
var feed=MyList.Items.Cast().ToList();
这是正确的方法。但是,请查看列表框中的项目类型。它们可能不是
string
类型,这就是您出现此错误的原因。您可能没有类型为
字符串的项目。将列表框中的T替换为该类型。或者修复列表框,使其包含类型为
string

的项目。我认为Sam的答案是正确的。如果ListBox的ItemsSource是字符串集合,那么将它们转换为字符串应该不会有问题

        MyList.ItemsSource = new List<string>
        {
            "one","two","three"
        };
        List<string> feed = MyList.Items.Cast<string>().ToList();
然后,您必须将其强制转换为适当的类型,并选择所需的属性

        MyList.ItemsSource = new List<MyClass>
        {
            new MyClass {MyProperty ="one"},
            new MyClass {MyProperty ="two"},
            new MyClass {MyProperty ="three"},
        };
        List<string> feed = MyList.Items.Cast<MyClass>().Select(c => c.MyProperty).ToList();
MyList.ItemsSource=新列表
{
新的MyClass{MyProperty=“one”},
新的MyClass{MyProperty=“two”},
新的MyClass{MyProperty=“three”},
};
List feed=MyList.Items.Cast().Select(c=>c.MyProperty.ToList();

但要正确回答您的问题,我们需要了解您的列表框的项目来源。

您会遇到错误,因为您的列表不是由字符串组成的!请尝试获取字符串表示形式

List<string> feed = MyList.Items.Cast<object>()
    .Select(o => o.ToString()).ToList();
List feed=MyList.Items.Cast()
.Select(o=>o.ToString()).ToList();

您知道,在wpf中,您有Listbox和ListboxItem,您必须先将您的项目兑换成ListboxItem。 在那之后。将listboxItem.Content推送到
List List=新列表();
foreach(this.MyList.Items中的var项)
{
ListBoxItem castItem=作为ListBoxItem的项目;
list.Add(castItem.Content.ToString());
}

在这里你可以试试

        MyList.ItemsSource = new List<MyClass>
        {
            new MyClass {MyProperty ="one"},
            new MyClass {MyProperty ="two"},
            new MyClass {MyProperty ="three"},
        };
        List<string> feed = MyList.Items.Cast<MyClass>().Select(c => c.MyProperty).ToList();
List<string> feed = MyList.Items.Cast<object>()
    .Select(o => o.ToString()).ToList();