简体   繁体   English

如何将ListBox.Items用作列表的Itemsource <String>

[英]How to use ListBox.Items as Itemsource of a List<String>

I have a ListBox . 我有一个ListBox I also have a list in code behind. 我在后面的代码中也有一个list I want to get all the items from the listbox as the source of my list . 我想从所有项目listbox作为我的源list

I tried this way: 我这样尝试:

<listBox x:Name="MyList" .../>

list<string> feed = new list<string>();

//to get the source
var feed = MyList.Items.Cast<string>().ToList();

but It's throwing an Exception: System.InvalidCastException 但是它抛出一个异常: System.InvalidCastException

what to do? 该怎么办?

You are getting the error because your list isn't made of strings! 因为列表不是由字符串组成的,所以出现错误! Try to get the string representation instead. 尝试获取字符串表示形式。

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

This is the correct way. 这是正确的方法。 But, see the type of items you have in your ListBox. 但是,请查看列表框中具有的项目类型。 They may not be string type That's why you are getting this error. 它们可能不是string类型,这就是为什么您会收到此错误。 You may not have items with the type string . 您可能没有类型为string项目。 Replace T with that type in your ListBox. 在您的ListBox中用该类型替换T。 Or fix your ListBox to have items with the type string . 或修复您的ListBox以使项目具有string类型。

I think Sam's answer is correct. 我认为山姆的答案是正确的。 If the ItemsSource of your ListBox is string collection, then casting them to string should not be a problem. 如果ListBox的ItemsSource是字符串集合,则将它们强制转换为字符串应该不是问题。

        MyList.ItemsSource = new List<string>
        {
            "one","two","three"
        };
        List<string> feed = MyList.Items.Cast<string>().ToList();

But if the ItemsSource of your ListBox is other than string collection, suppose MyClass collection 但是,如果ListBox的ItemsSource不是字符串集合,则假定MyClass集合

    class MyClass
    {
        public string MyProperty { get; set; }
        public override string ToString()
        {
            return this.MyProperty;
        }
    }

Then you have to cast it in appropriate type and select desired property. 然后,您必须将其转换为适当的类型并选择所需的属性。

        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();

But to answer your question correctly we need to know about the ItemsSource of your ListBox. 但是要正确回答您的问题,我们需要了解ListBox的ItemsSource。

You know in wpf you have Listbox has ListboxItem and you have to cash you item to ListboxItem first. 您知道在wpf中您具有Listbox的ListboxItem,并且必须先将商品兑现到ListboxItem。 And after that. 在那之后。 You push the listboxItem.Content to your List<string> list = new List<string>(); foreach (var item in this.MyList.Items) { ListBoxItem castItem = item as ListBoxItem; list.Add(castItem.Content.ToString()); } 您将listboxItem.Content推送到List<string> list = new List<string>(); foreach (var item in this.MyList.Items) { ListBoxItem castItem = item as ListBoxItem; list.Add(castItem.Content.ToString()); } List<string> list = new List<string>(); foreach (var item in this.MyList.Items) { ListBoxItem castItem = item as ListBoxItem; list.Add(castItem.Content.ToString()); }

Here you can try 在这里你可以尝试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM