Forum guidelines
Move Item from bound Listbox to another bound Listbox
Posted By Steven Davisworth on 13 Dec 2012
I've created two lisboxes. One is bound to an ObservableCollection returning results from SQLight query. The code I have in place works great if listbox2 is not bound.

Basic senario is Listbox1 presents selectable items to populate listbox2. User dblClick listbox1 item and it moves to listbox2. Listbox2 is saved to DB... when the user opens the UI at any given time or even in same session, they should be able to remove items from lisbox2. NOTE (Listbox1 is filtered by whatever is in Listbox2 i.e no items can exist in two listboxes at any given time.)

Problem: As soon as I try and bind listbox2 I get error "Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead."

Listbox1 MouseDoubleClick -
ListBoxItem myListBoxItem =
  // listBoxCertificateSelected.DataContext = null;

Listbox2 MouseDoubleClick -
ListBoxItem myListBoxItem =
 //listBoxCertificateLookup.DataContext = null;

Listbox1 -
 ObservableCollection<CertificateLookup> CertificateLookupList = new ObservableCollection<CertificateLookup>();
  foreach (DataRow r in conditions.Rows)
  CertificateLookupList.Add(new CertificateLookup()
  Section = r["Section"].ToString(),
  Description = r["Description"].ToString(),
  SortOrder = r["SortOrder"].ToString()
  listBoxCertificateLookup.DataContext = CertificateLookupList;

Listbox2 -
ObservableCollection<CertificateSelected> CertificateSelectedList = new ObservableCollection<CertificateSelected>();
  foreach (DataRow r in conditions.Rows)
  CertificateSelectedList.Add(new CertificateSelected()
  selectedSection = r["Section"].ToString(),
  selectedDescription = r["Description"].ToString(),
  selectedSortOrder = r["SortOrder"].ToString()
  listBoxCertificateSelected.DataContext = CertificateSelectedList;

... rest of code as attached.

Thanks in advance.

Re: Move Item from bound Listbox to another bound Listbox
Posted By Satyapriya Nayak on 13 Dec 2012
If this post helps you mark it as answer

Re: Move Item from bound Listbox to another bound Listbox
Posted By Steven Davisworth on 14 Dec 2012  
Thank you for your response
I've come across the above posting which I tried before, but am
not sure on how to apply data templates to that code so as to allow me to have columns filling up and then spilling over to second column and then third colomn etc. as items get added?

Apart from this the
general consensus is that I use an ObservableCollection

<ListBox Name="listBoxCertificatesLookup"
  ItemsSource="{Binding Tables[0]}"
  HorizontalAlignment="Left" Width="300" Height="75" >
  <StackPanel Height="23" Orientation="Horizontal">
  <TextBlock Name="textBlock" Text="{Binding Path=Description}" VerticalAlignment="Top" />
  <WrapPanel IsItemsHost="True" Orientation="Vertical" />

C# Corner Annual Conference 2016


Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.