Operation Is Not Valid While Itemssource Is in Use. Access and Modify Elements with Itemscontrol.Itemssource Instead

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

You're binding the ItemsSource to a property in the DataContext called Items, so to update the collection, you need to go to the Items property in the DataContext and clear it.

In addition, the Items property needs to be of type ObservableCollection, not List if you want it to update the UI whenever the underlying collection changes.

Your bit of code that sets the ItemsSource in the code behind is not needed and should be removed. You only need to set the ItemsSource in one place, not both.

Here's a simple example of how it can work:

// Using Students instead of Items for the PropertyName to clarify
public ObservableCollection<Student> Students { get; set; }

public MyConstructor()
{
...

Students = search.students();
listBoxSS.DataContext = this;
}

Now when you have:

<ListView ItemsSource="{Binding Students}" ... />

you're binding the ItemsSource to the ObservableCollection<Student>, and when you want to clear the list you can call:

Students.Clear()

Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead when execution twice

Thanks Sheridan. I have also discovered that if I do...

ObservableCollection<string> calledList = obj.GetList();
calledList.Clear(); // I have to use this line of code
calledList.ItemsSource = calledList;

This solve my problem. I am not using xaml because it gave me problems. You may remember I opened a thread about combobox when navigating through records. I managed to solve that problem by using for loop. have a look at my other thread, if you wish, here

However this is not the final solution. I am learning wpf and its cud operation so it will be interesting what I will discover

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

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);
}
}
}

Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead. Error while parsing XAML

After searching through all the questions related to this, I couldn't find the answer. Most involved people changing the Items property on the TreeViewItem, which will throw this error.

In my case, I didn't change anything.

However, if you notice.

<TreeViewItem Header="{Binding Header}" ItemsSource="{Binding DataGroups}">
<ListView ItemsSource="{Binding DataGroups}">

I have bound to the same collection twice.

So, double check you haven't bound to the same collection twice if you get this error.

Operation is not valid while ItemsSource is in use exception - what am I doing wrong?

You could try out something like this:

<Window.Resources>
<DataTemplate x:Key="inner">
<TextBlock Content="{Binding}"/>
</DataTemplate>

<DataTemplate x:Key="outer">
<ItemsControl ItemsSource="{Binding}" ItemTemplate="{DynamicResource inner}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</DataTemplate>

</Window.Resources>

<ItemsControl x:Name="itemControl" ItemsSource="{Binding Path=Nations, Source={StaticResource nationMetric}}" ItemTemplate="{DynamicResource outer}">

This basically identifies that there is a list within in a list and does the binding appropriately. I haven't tried it, but I think this is the approach!

Other resources:

WPF: How to create a custom items control panel?

Binding 2 dimensional Collection to some control

Operation is not valid while ItemsSource is in use. But i'm not mixing

The ItemsPanelTemplate should be defined in ItemsPanel of ListBox. Update your declaration as

   <ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>

You can read about ItemsPanel and how it works here.

WPF C# Unable to remove item from Listview (ItemsSource is in use)

You should remove the item from the ItemsSource:

var itemsSource = FilesList.ItemsSource as IList<YourClass>();
if (itemsSource != null)
itemsSource.Remove(selectedFile);

Note that you need to set the ItemsSource to an ObservableCollection<T>, or a custom collection that implements INotifyCollectionChanged, for the it to get removed from the view without you having to refresh it explicitly, i.e. removing an item from a List<T> won't update the view.

How to bind to a collection inside of a collection?

To put this in the form of an answer. Your XAML should be like what is below. The only change I made was to put your second DataTemplate inside an <ItemsControl.ItemTemplate> tag. Because that's why it's there; You are using it to set the ItemTemplate property of the ItemsControl around it.

<ItemsControl ItemsSource="{Binding MachinistUnreportedOps}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding MultiJobOps}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock>
<Run Text="Op " />
<Run Text="{Binding JobOperation}" />
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

WPF Data Grid : Operation is not valid while ItemsSource is in use.

For the inner dataGrid, you forgot to wrap the columns under <DataGrid.Columns> tag:

<DataGrid ItemsSource="{Binding Orders}" AutoGenerateColumns="False">
<DataGrid.Columns> <-- This is missing.
<DataGridTextColumn Header="Product Name" Binding="{Binding ProductName}" />
<DataGridTextColumn Header="Quantity" Binding="{Binding Quantity}" />
<DataGridTextColumn Header="Unit Price" Binding="{Binding UnitPrice}" />
<DataGridTextColumn Header="Total Price" Binding="{Binding TotalPrice}" />
</DataGrid.Columns>
</DataGrid>


Related Topics



Leave a reply



Submit