使用 for 循环而不是 foreach 循环遍历 wpf 列表框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24062850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:51:06  来源:igfitidea点击:

Loop through wpf listbox using a for loop instead of a foreach loop

c#wpflistbox

提问by Jan Solo

I have a c# wpf listbox and I am trying to get the values from the selected items. I cannot use a foreach loop (every value I find will remove an item from the listbox). But this seems impossible.

我有 ac# wpf 列表框,我正在尝试从所选项目中获取值。我不能使用 foreach 循环(我找到的每个值都会从列表框中删除一个项目)。但这似乎是不可能的。

What I want is somthing like this:

我想要的是这样的东西:

for (int i = <numberofselecteditems> - 1; i >= 0; i--)
{
    string displaymembervalue = listbox.selecteditem[i].displaymembervalue;

}

I have a solution which involve to loop over all the listbox items twice. This is not really an option since it will slow the app too much.

我有一个解决方案,它涉及两次遍历所有列表框项目。这不是一个真正的选择,因为它会使应用程序减慢太多。

Like I said before, this is NOT the

就像我之前说的,这不是

System.Windows.Forms.Listbox

but the

但是

System.Windows.Controls.Listbox

thank you!!

谢谢你!!

J.

J。

回答by GEEF

See the solution here, it is essentially using a foreach in the follolwing fashion:

请参阅此处的解决方案,它本质上是以以下方式使用 foreach:

foreach (var item in listBox1.SelectedItems)
{
    // Do what you want here... Console.WriteLine(item), etc.
}

If you really want to do it with a for loop rather than a foreach, then do the following:

如果你真的想用 for 循环而不是 foreach 来做,那么执行以下操作:

for(int i = selectedItems.Count - 1; i >= 0; --i)
{
    var item = selectedItems[i];
    // Do what you want with item
}

回答by Anthony Russell

Here is your XAML bound to a Observable collection

这是绑定到 Observable 集合的 XAML

  <ListBox ItemsSource="{Binding items}"/>

Here is your observable collection of objects

这是您的可观察对象集合

private ObservableCollection<Object> _items;

public ObservableCollection<Object> items{
  get{ return _items; }
}

Here is the enumeration over them and the removing of each item

这是对它们的枚举和每个项目的删除

for(int x = 0; x < _items.Count; x++){
   _items.Remove(_items.Where(n => n == _items[x]).Single());
   //You may have to do a notify property changed on this if the UI Doesnt update but thats easily googled.
   //Traditionally it would update. However since you are bound to items Im not sure if it will update when you manipulate _items
}

回答by IdahoSixString

Alternatively instead of enumerating twice you can create a copy of the list of objects and then remove the items from the original list while still enumerating.

或者,您可以创建对象列表的副本,然后在仍然枚举的同时从原始列表中删除项目,而不是枚举两次。

        foreach (var selectedItem in listBox1.SelectedItems.Cast<List>())
        {
            //remove items from the original list here
        }

回答by Brian Watt

Create a second list. You still have to iterate twice, but the second iteration is not over the entire list of items.

创建第二个列表。您仍然需要迭代两次,但第二次迭代不会遍历整个项目列表。

var items List<ListBoxItem>;
foreach (var item in listbox1.SelectedItems)
    items.Add(item);

foreach (var item in items)
    listbox1.Remove(item);