Click here to Skip to main content
15,885,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am pretty new to C# WPF, so please bear with my question. I have two listboxes (listbox1 and listbox2) where the items in listbox1 will be added or removed during runtime through user input. I want the listbox2 to display its listboxitem accordingly through binding method.

For example, if listbox1 has 5 items initially, i want the listbox2 to display the same 5 items. If items in listbox1 being added or removed in runtime, I want the listbox2 to display the same data (items) as listbox1.

Can someone give me a tip? Thanks in advance.
Posted
Comments
jaideepsinh 8-Sep-14 10:41am    
You can bind both listbox with same data. Means use same DataSource for both listbox.
Ashi0891 8-Sep-14 11:56am    
You are doing same operation on both listbox.
Anand Gunasekaran 8-Sep-14 14:29pm    
private static void btnAdd_Click(object sender,EventArgs e){
Listbox1.Items.Add("AAAA");
Listbox2.Items.Add("AAAA");
}

private static void btnRemove_Click(object sender,EventArgs e){
Listbox1.Items.SelectedItem[0].Remove();
Listbox2.Items.SelectedItem[0].Remove();
}

1 solution

It's iether what jaideepsinh say and bind both of them to your original data. Or you bind listbox2 to listbox1 like so

XML
<ListBox Name="listbox1" ItemsSource="{Binding yourdatasource}">
 </ListBox>
 <ListBox ItemsSource="{Binding ElementName=listbox1, Path=ItemSource}">
 </ListBox>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900