Move items betwen listboxes.

Frank Suero 21 Reputation points
2023-01-06T17:51:27.847+00:00

I have two list boxes 1 and 2 plus two action buttons. The behavior I want is the following: I select an element in listbox 1, press the Add button and the element is added to listbox 2, automatically deleting that element from listbox 1. The same behavior is required by the list boxlist 2 with a Delete button.
What I've done with the code behind but it gives me an error saying that an item cannot be deleted because the ItemSource is busy.

How can I do this?

Thank you.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,813 questions
0 comments No comments
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,331 Reputation points
    2023-01-06T18:51:57.68+00:00

    Hi,
    try following demo:

    <Window x:Class="WpfApp1.Window039"  
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
            xmlns:local="clr-namespace:WpfApp039"  
            mc:Ignorable="d"  
            Title="FrankSuero-2266_230106" Height="450" Width="800">  
      <Window.DataContext>  
        <local:ViewModel/>  
      </Window.DataContext>  
      <Grid>  
        <Grid.ColumnDefinitions>  
          <ColumnDefinition/>  
          <ColumnDefinition/>  
        </Grid.ColumnDefinitions>  
        <Grid.RowDefinitions>  
          <RowDefinition Height="*"/>  
          <RowDefinition Height="Auto"/>  
        </Grid.RowDefinitions>  
        <ListBox ItemsSource="{Binding View1}" SelectedItem="{Binding LeftRow}" DisplayMemberPath="Text" Margin="10"/>  
        <ListBox Grid.Column="1" ItemsSource="{Binding View2}" SelectedItem="{Binding RightRow}" DisplayMemberPath="Text" Margin="10"/>  
        <Button Grid.Row="1" Content="to right" Command="{Binding}" CommandParameter="LeftRight" Margin="10" Width="100"/>  
        <Button Grid.Row="1" Grid.Column="1" Content="to left" Command="{Binding}" CommandParameter="RightLeft" Margin="10" Width="100"/>  
      </Grid>  
    </Window>  
    

    and ViewModel:

    using System;  
    using System.Collections.ObjectModel;  
    using System.ComponentModel;  
    using System.Windows;  
    using System.Windows.Data;  
    using System.Windows.Input;  
      
    namespace WpfApp039  
    {  
     public class ViewModel:ICommand  
     {  
     public ViewModel()  
     {  
     for (int i = 1; i < 10; i++)  
     {  
     col1.Add(new Data() { ID = i, Text = $"Col1 - {i}" });  
     col2.Add(new Data() { ID = i, Text = $"Col2 - {i}" });  
     }  
     cvs1.Source = col1;  
     cvs2.Source = col2;  
     }  
     private ObservableCollection<Data> col1 = new();  
     private ObservableCollection<Data> col2 = new();  
     private CollectionViewSource cvs1 = new();  
     private CollectionViewSource cvs2 = new();  
      
     public ICollectionView View1 { get => cvs1.View; }  
     public ICollectionView View2 { get => cvs2.View; }  
      
     public Data LeftRow { get; set; }  
     public Data RightRow { get; set; }  
      
     public event EventHandler? CanExecuteChanged;  
     public bool CanExecute(object? parameter) => true;  
      
     public void Execute(object? parameter)  
     {  
     switch (parameter?.ToString())  
     {  
     case "LeftRight":  
     if (LeftRow != null)  
     {  
     Data d = LeftRow;  
     col1.Remove(d);  
     col2.Add(d);  
     }  
     break;  
     case "RightLeft":  
     if (RightRow != null)  
     {  
     Data d = RightRow;  
     col2.Remove(d);  
     col1.Add(d);  
     }  
     break;  
     default:  
     break;  
     }  
     }  
     }  
      
     public class Data  
     {  
     public int ID { get; set; }  
     public string Text { get; set; }  
     }  
    }  
      
    

    Result:

    276920-x.gif

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.