I’m trying to link a 2d list of objects to a contentcontrol. I’m getting an exception that I don’t understand. Could someone clarify it for me? (I tried setting the first.ItemsSource property in C# as well to a NationMetrics object that I declared in code – it gave me the same exception)
Outer Exception:
Add value to collection of type ‘System.Windows.Controls.ItemCollection’ threw an exception.
Inner Exception:
Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.
The NationMetrics class:
public class NationMetrics
{
List<List<Field>> _Nations = new List<List<Field>>();
public List<List<Field>> Nations { get { return _Nations; } set { _Nations = value; } }
}
Here’s the snippet from my MainWindow.xaml where the exception is being triggered
<Grid>
<ItemsControl x:Name="first" ItemTemplate="{DynamicResource DataTemplate_Level1}" ItemsSource="{Binding Path=Nations, Source={StaticResource nationMetric}}" />
</Grid>
Here’s the snippet from my Window.Resources which defines nationMetric
<local:NationMetrics x:Name="nm" x:Key="nationMetric" />
You could try out something like this:
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