I am using Canvas in WPF Window to show UserControl.
I dont want to add the same UserControl in the Canvas.
How can i do that?
Current i have done..
private void OpenChild(UserControl ctrl)
{
ctrl.Uid = ctrl.Name;
if (JIMSCanvas.Children.Count == 0)
{
JIMSCanvas.Children.Add(ctrl);
}
else
{
foreach (UIElement child in JIMSCanvas.Children)
{
if (child.Uid == ctrl.Uid)
{
MessageBox.Show("Already");
}
else
{
JIMSCanvas.Children.Add(ctrl);
}
}
}
}
and adding a UserControl like this
OpenChild(new JIMS.View.Ledger());
It works for me but when i add an other Control like
OpenChild(new JIMS.View.Stock());
Its throwing an exception called
The enumerator is not valid because the collection changed.
The error is due to the fact that you’re modifying the enumeration while in the process of cycling through it (inside the foreach). Just have to use a method that doesn’t alter the enumeration: