Remove ListItem in silverlight - c#

I have a ListBox in my Silverlight project.And,when to remove and add ListItem from a ListBox,I got the following error.
Operation not supported on read-only collection.
Code:
public void btnUp_Click(object sender, RoutedEventArgs e)
{
if (lbChoices.SelectedItem != null)
{
ListBoxItem selectedItem = new ListBoxItem();
selectedItem.Content = lbChoices.SelectedItem;
selectedItem.IsSelected = true;
int selectedIndex = lbChoices.SelectedIndex;
if (lbChoices.Items.Count > 1)
{
if (selectedIndex > 0)
{
lbChoices.Items.Remove(lbChoices.SelectedItem);
lbChoices.Items.Insert(selectedIndex - 1, selectedItem);
}
}
}
}

When you are using ItemsControl with an ItemsSource, you can not add/remove elements using the Items collection. You should modify your underlying collection instead.
"The problem stems from the fact that I’d bound my ListBox to an ObservableCollection, once bound the Items collection becomes read-only."

I guess you added items by binding the ItemsSource? If so, remove the item from the collection you are binding to.

You need to remove the item from the source that your ListBox is bound to not the ListBox itself. As soon as your remove it from the source, the ListBox will automatically refresh to not display the item.

Change your code like this:
private void button1_Click(object sender, RoutedEventArgs e)
{
if (lbChoices.SelectedItem != null)
{
ListBoxItem selectedItem = (ListBoxItem)lbChoices.SelectedItem;
int selectedIndex = lbChoices.SelectedIndex;
if (lbChoices.Items.Count > 1)
{
if (selectedIndex > 0)
{
lbChoices.Items.Remove(lbChoices.SelectedItem);
lbChoices.Items.Insert(selectedIndex - 1, selectedItem);
}
}
}
}
It seems that your are moving up the selected item in the list box.

Related

Delete selected item from listbox with button from window form

I have a listbox and i want to select and item in it and press a button to delete that from the database. I can edit and save fine just not delete.
Current code:
private void button1_Click_3(object sender, EventArgs e)
{
if (listBox1.Items.Count >= 1)
{
if (listBox1.SelectedValue != null)
{
listBox1.Items.Remove(listBox1.SelectedItem);
System.Windows.Forms.MessageBox.Show("Item Deleted");
}
}
else
{
System.Windows.Forms.MessageBox.Show("No ITEMS Found");
}
}
I am getting the error:
Items collection cannot be modified when the DataSource property is set.
private void button1_Click_3(object sender, EventArgs e)
{
if (listBox1.Items.Count >= 1)
{
if (listBox1.SelectedValue != null)
{
var items = (List<YourType>)listBox1.DataSource;
var item = (YourType)listBox1.SelectedValue;
listBox1.DataSource = null;
listBox1.Items.Clear();
items.Remove(item);
listBox1.DataSource = items;
}
}
else
{
System.Windows.Forms.MessageBox.Show("No ITEMS Found");
}
}
This will work
Assuming there can be multiple items selected on the ListBox. Below logic will remove from the UI. Also, you can add logic to remove the items from the database if required.
Below exception occurs when Data Bindings are different than data sources.
Items collection cannot be modified when the DataSource property is
set
A data source specifies the source of data whereas data bindings define how data is bound to once it is retrieved. Typically, data binding occurs only during UI initialisation, and it doesn't change after that.
The data source itself can change over the life of control, but this is rare.
Here, the data source elements change.
On to your actual error. You cannot set DataSource and modify the Items property. It isn't allowed. The correct way to handle this is to add the items to your data source. This insertion in your data source will cause the control to update the list automatically. The ListBox Control relies on the DS to store the data so all changes must go through that. In the rare case where you need to be able to edit the Items directly then, you'll have to do away with DataSource and manually populate the Items collection. This update to DataSource usually is not needed though.
Below code to reset the data source
if(listBox1.SelectedItems != null)
{
// Assuming its List of string
var items = listBox1.DataSource as List<string>;
// Remove multiple selected items
var count = listBox1.SelectedItems.Count;
while(count != 0)
{
var selectedItem = listBox1.SelectedItems[count-1];
if(items.ContainsKey(selectedItem))
{
items.Remove(selectedItem);
}
count--;
}
listBox1.DataSource = null;
listBox1.Items.Clear();
listBox1.DataSource = items;
}
An optimised approach to store the data source and modify it to update the data source directly. Use the ObservableCollection type and update the collection to update the UI.
public class NamesClass : DependencyObject
{
public ObservableCollection<string> Names {get; private set; }
public TestClass()
{
this.Names = new ObservableCollection<string>();
}
}
Now include the DependencyObject class as DataSource and update the Names collection.

Changing SelectedItem property of a ListView programatically

I have a 2 ListViews with same items in both of them. What I want to do is that when a selection is made in one ListView, the same selection should be reflected in the other ListView also. The two ListViews are bound to two different ViewModels but both the ViewModels implement the same interface.
I've overridden the Equals methods in both ViewModels.
The two ListViews are on different XAML pages. The first ListView say LV1 is in Page1.xaml and LV2 is in Page2.xaml. What I want is that when I am changing the selection in LV2 the selection in LV1 should also change( one way only ). I've set x:FieldModifier="public" on LV1 and exposing through a static property of Page1 like this:
public sealed partial class Page1 : Page
{
public static Page1 page1 { get; private set; }
}
And on Page2, I have this :
private async void LV2_ItemClick(object sender, ItemClickEventArgs e)
{
var selected = e.ClickedItem as ISomeCommonInterface;
//Comparision is successful --> Contains() always returns corect value;
if (Page1.page1.LV1.Items.ToList().Contains(selected))
{
Page1.page1.LV1.SelectedItem = null; // this works
Page1.page1.LV1.SelectedItem = selected; // this doesn't work
}
}
I've found that inside the if condition, assignment to null changes the SelectedItem of LV1 to null but the next line doesn't change it to selected ( it remains null ).
add after assignment:
Page1.page1.LV1.Select();
This works for me:
private void LV1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selected = (sender as ListView).SelectedItem as string;
int index = -1;
for (int i = 0; i < LV2.Items.Count(); i++)
{
if (LV2.Items[i] as string == selected){
index = i;
break;
}
}
// The if becomes obsolete here, it could be replaced by
// if(index >= 0)
if (LV2.Items.ToList().Contains(selected))
{
LV2.SelectedIndex = index;
}
}
There is probably an easier way of getting the index of LV1's SelectedItem in LV2, but it should be enough to get you on the right track.
You can check out the minimal testing app I created that shows that SelectedItem works too.
Method 1 - SelectionMode="Multiple" - both ListViews in sync
You should subscribe the SelectionChanged event on both ListViews - item may not get selected only by click - and there (when selection is changed) you should sync the selection.
private void SyncSelection(object sender, SelectionChangedEventArgs e)
{
ListView listViewToAdd = ReferenceEquals(sender, firstListView) ? secondListView : firstListView;
foreach (var item in e.AddedItems)
{
if (!listViewToAdd.SelectedItems.Contains(item))
{
listViewToAdd.SelectedItems.Add(item);
}
}
foreach (var item in e.RemovedItems)
{
listViewToAdd.SelectedItems.Remove(item);
}
}
Method 2 - SelectionMode="Multiple" - update one after selecting in the other
You should subscribe the SelectionChanged event only on the ListView where items could be selected.
private void SyncSelection(object sender, SelectionChangedEventArgs e)
{
foreach (var item in e.AddedItems)
{
secondListView.SelectedItems.Add(item);
}
foreach (var item in e.RemovedItems)
{
secondListView.SelectedItems.Remove(item);
}
}
Method 3 - SelectionMode="Single"
Subscribe the SelectionChanged event on both if you want to make them be in sync or only on the selectable one if you only want to update the second based on the first.
private void SyncSelection(object sender, SelectionChangedEventArgs e)
{
ListView senderListView = (ListView)sender;
ListView listViewToAdd = ReferenceEquals(sender, firstListView) ? secondListView : firstListView;
listViewToAdd.SelectedItem = senderListView.SelectedItem;
}
You may need to replace var with your interface to make it work.

Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource

I'm trying to make 2 list boxes where I can press on a button to add an item selected from the left listbox to the right listbox. Here's the XAML for the listboxes:
<ListBox
x:Name="LeftList"
Foreground="{StaticResource Foreground}"
HorizontalAlignment="Left"
Height="237" Margin="15,103,0,0"
VerticalAlignment="Top"
Width="128">
<ListBoxItem>360T</ListBoxItem>
<ListBoxItem>BARX</ListBoxItem>
<ListBoxItem>BNP</ListBoxItem>
<ListBoxItem>BOA</ListBoxItem>
<ListBoxItem>CITI</ListBoxItem>
<ListBoxItem>CS</ListBoxItem>
<ListBoxItem>DB</ListBoxItem>
<ListBoxItem>GS</ListBoxItem>
<ListBoxItem>JPM</ListBoxItem>
<ListBoxItem>RBS</ListBoxItem>
<ListBoxItem>UBS</ListBoxItem>
</ListBox>
<ListBox
x:Name="RightList"
Foreground="{StaticResource Foreground}"
HorizontalAlignment="Left"
Height="237" Margin="257,103,0,0"
VerticalAlignment="Top"
Width="128"/>
C#:
List<string> leftSideList = new List<string>();
List<string> rightSideList = new List<string>();
public ChooseLPWindow()
{
InitializeComponent();
//Add to the collection leftside list
leftSideList.Add("360T");
leftSideList.Add("BARX");
leftSideList.Add("BNP");
leftSideList.Add("BOA");
leftSideList.Add("CITI");
leftSideList.Add("CS");
leftSideList.Add("DB");
leftSideList.Add("GS");
leftSideList.Add("JPM");
leftSideList.Add("RBS");
leftSideList.Add("UBS");
}
private void AddBtn_Click(object sender, RoutedEventArgs e)
{
if (LeftList.SelectedIndex > -1)
{
int SelectedIndex = LeftList.SelectedIndex;
string SelectedItem = LeftList.SelectedValue.ToString();
//Add the selected item to the right side list
RightList.Items.Add(SelectedItem);
rightSideList.Add(SelectedItem);
if (leftSideList != null)
{
//Remove the item from the collection list
leftSideList.RemoveAt(SelectedIndex);
//Update the left side list
LeftList.Items.Clear();
LeftList.ItemsSource = leftSideList;
}
}
}
I get the exception on:
LeftList.Items.Clear();
This happens when I try to add a second item the first one gets added but then the exception occurs when you try to add another item. The error is:
Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource
Any suggestions?
You can't modify ListBox's Items when the items populated through ItemsSource. In that case you suppose to modify items in the ItemsSource collection instead.
I'd suggest to change your List to ObservableCollection. With that removing item from collection is enough, because ObservableCollection has built-in mechanism to notify UI to refresh whenever item added or removed from collection :
ObservableCollection<string> leftSideList = new ObservableCollection<string>();
ObservableCollection<string> rightSideList = new ObservableCollection<string>();
public ChooseLPWindow()
{
InitializeComponent();
leftSideList.Add("360T");
leftSideList.Add("BARX");
leftSideList.Add("BNP");
leftSideList.Add("BOA");
leftSideList.Add("CITI");
leftSideList.Add("CS");
leftSideList.Add("DB");
leftSideList.Add("GS");
leftSideList.Add("JPM");
leftSideList.Add("RBS");
leftSideList.Add("UBS");
LeftList.ItemsSource = leftSideList;
}
private void AddBtn_Click(object sender, RoutedEventArgs e)
{
if (LeftList.SelectedIndex > -1)
{
int SelectedIndex = LeftList.SelectedIndex;
string SelectedItem = LeftList.SelectedValue.ToString();
//Add the selected item to the right side list
RightList.Items.Add(SelectedItem);
rightSideList.Add(SelectedItem);
if (leftSideList != null)
{
//Remove the item from the ItemsSource collection
//instead of removing it from ListBox.Items
leftSideList.RemoveAt(SelectedIndex);
}
}
}
I fixed the issue by doing this:
private void AddBtn_Click(object sender, RoutedEventArgs e)
{
if (LeftList.SelectedIndex > -1)
{
int SelectedIndex = LeftList.SelectedIndex;
string SelectedItem = LeftList.SelectedValue.ToString();
//Add the selected item to the right side list
RightList.Items.Add(SelectedItem);
rightSideList.Add(SelectedItem);
//Delete the item from the left side list
//ListLps.Items.RemoveAt(SelectedIndex);
if (leftSideList != null)
{
//Remove the item from the collection list
leftSideList.RemoveAt(SelectedIndex);
LeftList.Items.RemoveAt(SelectedIndex);
}
}
}

Listbox items at the bottom were not automatically selected

I have this subtle program regarding the behavior of listbox. My listbox is binded with an observable list in the viewmodel. There are 2 ways in addding an item in the listbox. First is ADD a single item then that item would be selected directly. This works fine.
The second way was LOAD which by its name will be adding more than 1 item in the lisbox. Now the problem is when loading items more than the listbox can accomodate in the view, those items that are not in view (items at the bottom thus need to be scrolled in order for it to be viewed) was not automatically selected...
Only the items that are by default viewed are the ones selected:
private void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
{
if (listBoxAddresses.ItemContainerGenerator.Status != GeneratorStatus.ContainersGenerated) return;
for (int i = 0; i < TestSetting.DeviceSettings.Count; i++)
{
ListBoxItem myListBoxItem = (ListBoxItem)(listBoxAddresses.ItemContainerGenerator.ContainerFromItem(TestSetting.DeviceSettings[i]));
if (myListBoxItem != null)
{
myListBoxItem.IsSelected = true;
}
}
listBoxAddresses.ItemContainerGenerator.StatusChanged -= ItemContainerGenerator_StatusChanged;
}
I wonder if this is just a natural behavior for listbox.
I just realize this now...setting my listbox to :
VirtualizingStackPanel.IsVirtualizing="False"
did all the trick. Thanks to Dr.WPF for the idea. Though there are consequences for turning off virtualization (performance) but it won't matter that much.

ListBox Item Removal

I have a WPF window that manages sets of configurations and it allows users to edit a configuration set (edit button) and to remove a configuration set (remove button). The window has a ListBox control that lists the configuration sets by name and its ItemsSource has a binding set to a list of configuration sets.
I'm trying to remove the item in the code behind file for the window..
private void RemoveButton_Click(object sender, RoutedEventArgs e)
{
var removedItems = configSetListBox.SelectedItems;
foreach(ConfigSet removedItem in removedItems)
{
configSetListBox.Items.Remove(removedItem);
}
}
My code yields an invalid operation exception stating "Access and modify elements with ItemsControl.ItemsSource instead." What property should I be accessing to properlyremove items from the ListBox? Or is there possibly a more elegant way to handle this in WPF? My implementation is a bit WinForm-ish if you will :)
Solution
private void RemoveButton_Click(object sender, RoutedEventArgs e)
{
foreach(ConfigSet removedItem in configSetListBox.SelectedItems)
{
(configSetListBox.ItemsSource as List<ConfigSet>).Remove(removedItem);
}
configSetListBox.Items.Refresh();
}
In my case I had a List as the ItemSource binding type so I had to cast it that way. Without refreshing the Items collection, the ListBox doesn't update; so that was necessary for my solution.
use:
private void RemoveButton_Click(object sender, RoutedEventArgs e)
{
foreach(ConfigSet item in this.configSetListBox.SelectedItems)
{
this.configSetListBox.ItemsSource.Remove(item); // ASSUMING your ItemsSource collection has a Remove() method
}
}
Note: my use of this. is just so it as it is more explicit - it also helps one see the object is in the class namespace as opposed to variable in the method we are in - though it is obvious here.
This is because , you are modifying a collection while iterating over it.
if you have binded item source of listbox than try to remove the items from the source
this was answered here already.
WPF - Best way to remove an item from the ItemsSource
You will need to implement an ObservableCollection and then whatever you do to it will be reflected in your listbox.
I Used this logic to preceed. And it worked.
you may want to try it.
private void RemoveSelectedButton_Click(object sender, RoutedEventArgs e) {
if (SelectedSpritesListBox.Items.Count <= 0) return;
ListBoxItem[] temp = new ListBoxItem[SelectedSpritesListBox.SelectedItems.Count];
SelectedSpritesListBox.SelectedItems.CopyTo(temp, 0);
for (int i = 0; i < temp.Length; i++) {
SelectedSpritesListBox.Items.Remove(temp[i]);
}
}
for (int i = lstAttachments.SelectedItems.Count - 1; i >= 0; i--)
{
lstAttachments.Items.Remove(lstAttachments.SelectedItems[i]);
}
Simplest way to remove items from a list you iterate through is going backwards because it does not affect the index of items you are moving next to.

Categories