Question

I have a datagrid with an itemssource

ItemsSource="{Binding Path=SelectedCollection, Mode=TwoWay}"

when I set AutogenerateColums = true, everything works. But when I set it to alse add the following line to show just 1 column:

<DataGridTextColumn Width="2*" Binding="{Binding Path=Domain}" Header="Domein"/>

Exception:

Add value to collection of type 'System.Windows.Controls.ItemCollection' threw an exception Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.

I'm not trying to add anything to the collection anywhere.

On the same page I have another datagrid with a different collection that just works the way I described here above without any problems. The only differences I can think of is that the second collection is made up of object in another namespace but it should just exist in the same datacontext.

Anyone any ideas?

EDIT: In design time I get the following message: Cannot resolve property Domain in Data Context of type ....

Was it helpful?

Solution

You should add custom column inside Columns tag. I suspect you are adding directly under DataGrid tag. It should be:

<DataGrid ItemsSource="{Binding Path=SelectedCollection, Mode=TwoWay}">
  <DataGrid.Columns>
     <DataGridTextColumn/>
  </DataGrid.Columns>
</DataGrid>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top