Note that there are some explanatory texts on larger screens.

plurals
  1. POMVVM WPF ObservableCollection : Error addign items, ItemsSource binding
    text
    copied!<p>I'm having troubles adding items to an ObservableCollection while using a listbox with ItemsSource. I add dummy data for testing in my viewmodels constructor.</p> <p>My viewmodel: </p> <pre><code>public class KabaDeviceListViewModel : KabaBase { private ObservableCollection&lt;KabaDeviceDetailViewModel&gt; _details; public ObservableCollection&lt;KabaDeviceDetailViewModel&gt; KabaDevices { get { return _details; } set { if (value != _details) { _details = value; OnPropertyChanged("KabaDevices"); } } } public KabaDeviceListViewModel() { ObservableCollection&lt;KabaDeviceDetailViewModel&gt; _details = new ObservableCollection&lt;KabaDeviceDetailViewModel&gt;(); KabaDevice kd1 = new KabaDevice("localhost A", "127.0.0.1", true); KabaDeviceDetailViewModel dvm = new KabaDeviceDetailViewModel(kd1); _details.Add(dvm); KabaDevice kd2 = new KabaDevice("localhost B", "127.0.0.1", true); KabaDeviceDetailViewModel dvm2 = new KabaDeviceDetailViewModel(kd2); _details.Add(dvm2); this.KabaDevices = _details; } } </code></pre> <p>So far so good, but an error occurs in XAML code here, on ItemsSource of the listbox. I don't see what I'm doing wrong. I use VS2010 and .NET 4.0.</p> <pre><code>&lt;UserControl x:Class="KabaTest.View.KabaDeviceListView" ... xmlns:myViewModels="clr-namespace:KabaTest.ViewModel" xmlns:myViews="clr-namespace:KabaTest.View"&gt; &lt;UserControl.DataContext&gt; &lt;myViewModels:KabaDeviceListViewModel/&gt; &lt;/UserControl.DataContext&gt; &lt;Grid&gt; &lt;ListBox Margin="5" ItemsSource="{Binding Path=KabaDevices, Mode=TwoWay}" &gt; ScrollViewer.HorizontalScrollBarVisibility="Disabled" &gt; &lt;ListBox.ItemsPanel&gt; &lt;ItemsPanelTemplate&gt; &lt;WrapPanel IsItemsHost="True" Orientation="Horizontal" /&gt; &lt;/ItemsPanelTemplate&gt; &lt;/ListBox.ItemsPanel&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate DataType="{x:Type myViewModels:KabaDeviceDetailViewModel}" &gt; &lt;myViews:KabaDeviceDetailView DataContext="{Binding }"/&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; &lt;/Grid&gt; </code></pre> <p></p> <p>The InnerException at ItemsSource is : {"Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead."}. Thanks for your help!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload