Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you may be confused about how data binding works. When you bind a property, you are telling WPF to go look somewhere else for the value of that property.</p> <p>When you bind the <code>ListBox.ItemsSource</code> property to <code>currentMyItems</code>, you are telling WPF to go look at the <code>currentMyItems</code> list to find its list of items. If <code>currentMyItems</code> is an <code>ObservableCollection</code> instead of a <code>List&lt;T&gt;</code>, then the UI will automatically receive a notification to update the bound value when you add or remove an item from the collection.</p> <p>Based on what you say in the question, it sounds like you you have two collections, one of which is bound, and the other which is used to recreate the first collection anytime a change occurs. All that is not needed.</p> <p>Just create one <code>ObservableCollection&lt;MyItem&gt;</code>, bind it to the <code>ListBox.ItemsSource</code> property, and then add or remove items from that single collection. It should work as you would expect.</p> <pre><code>&lt;ListBox x:Name="listMyItems" ItemsSource="{Binding MyItems}" /&gt; </code></pre> <p>and</p> <pre><code>MyItems.Add((MyItem)listMyItems.SelectedItem) MyItems.Remove((MyItem)listMyItems.SelectedItem) </code></pre> <p>If you're interested, I also have some beginner articles on my blog for WPF users who are struggling to understand the DataContext. You may want to check out <a href="http://rachel53461.wordpress.com/2012/10/12/switching-from-winforms-to-wpfmvvm/" rel="noreferrer">Understanding the change in mindset when switching from WinForms to WPF</a> and <a href="http://rachel53461.wordpress.com/2012/07/14/what-is-this-datacontext-you-speak-of/" rel="noreferrer">What is this “DataContext” you speak of?</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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