WPF 使用现有的 ItemsSource 将对象添加到 ListBox
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27348796/
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
WPF Adding an object to ListBox with existing ItemsSource
提问by Swidtter
Hey I have a list box that I set the ItemsSource to be an ObservableCollection of objects from my database, and I need to add a single object at the end of this list. However I keep getting an invalid operation exception. Somehow my listbox is in use (which in my mind is a given as it is displayed and already have items inside.) Here is my code for the list box:
嘿,我有一个列表框,我将 ItemsSource 设置为数据库中对象的 ObservableCollection,我需要在此列表的末尾添加一个对象。但是我不断收到无效的操作异常。不知何故,我的列表框正在使用中(在我看来这是一个给定的,因为它显示出来并且里面已经有项目。)这是我的列表框代码:
<ListBox x:Name="CarList" SelectionChanged="ItemSelected" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Background="{x:Null}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel FlowDirection="LeftToRight" ItemHeight="300" ItemWidth="300"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="10,10">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="35" />
</Grid.RowDefinitions>
<Image Grid.Row="0" Source="{Binding image_path}" VerticalAlignment="Stretch"/>
<Grid Grid.Row="1" Background="SteelBlue">
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Center" Margin="3" Text="{Binding model}"/>
<TextBlock HorizontalAlignment="Right" VerticalAlignment="Center" Margin="3" Text="{Binding price}"/>
</Grid>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
And I first set the ItemsSource like so:
我首先像这样设置 ItemsSource:
CarList.ItemsSource = CarController.GetAllCars();
And then want to add my custom object like this:
然后想像这样添加我的自定义对象:
ListBoxItem carAdd = new ListBoxItem();
carAdd.Content = new CarModel{ image_path = "/../Assets/add-512.png", id=-1};
CarList.Items.Add(carAdd);
But alas the last operation fails with this message:
但可惜最后一次操作失败并显示以下消息:
Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.
使用 ItemsSource 时操作无效。改为使用 ItemsControl.ItemsSource 访问和修改元素。
I have looked for a few other suggestions but all use strings and single bindings in their examples and thus I haven't been able to figure out what exactly to do. If anyone got a suggestion it would be much appreciated.
我已经寻找了一些其他建议,但都在他们的示例中使用了字符串和单个绑定,因此我无法弄清楚到底要做什么。如果有人得到建议,将不胜感激。
- Thanks.
- 谢谢。
回答by Thomas Levesque
You need to add the item to the items source, and the source should be observable so that the ListBoxtakes the new item into account:
您需要将项目添加到项目源,并且源应该是可观察的,以便ListBox将新项目考虑在内:
var cars = new ObservableCollection<CarModel>(CarController.GetAllCars());
CarList.ItemsSource = cars;
...
var car = new CarModel{ image_path = "/../Assets/add-512.png", id=-1};
cars.Add(car);

