Patrick
Patrick

Reputation: 2583

C# WPF error Specified Visual is already a child of another Visual or the root of a CompositionTarget

Ok this is a fairly common error and I'm aware that basically I'm adding twice the same path to the canvas.

The problem is that I have a "dummy path" (let's call it pathDummy) which is added several times to the canvas any time changing it's geometry (that is its set of points). This is how I have to deal with it and can't remove it all the times.

So I tried to make a new path from pathDummy by doing:

System.Windows.Shapes.Path newPath = SelectedPath; paths.Add(newPath); plotCanvas.Children.Add(paths[paths.Count - 1]);

but that didn't solve the problem. So what is that stays the same in newPath and generates the error? thanx for any help

Patrick

Upvotes: 1

Views: 2884

Answers (2)

StayOnTarget
StayOnTarget

Reputation: 13008

In the case where the resource is within a resource dictionary, you may be able to mark the resource as not "shared" - see answer to this other question:

https://stackoverflow.com/a/35609066/3195477

I have found that this will avoid the cited error message.

Upvotes: 1

Giangregorio
Giangregorio

Reputation: 1510

You are not creating a new Path but with System.Windows.Shapes.Path newPath = SelectedPath;you are assigning to newPath your existing one. You need to create a brand new Path and assign to its data the geometry.

Upvotes: 1

Related Questions