WPF: How to add 3D object from xaml file to my viewport3D?
I have viewport3D and I want to add multiple 3d objects defined in xaml files to this Viewport3D, but by default 3d object deffined in .xaml file comes already with viewport3D and I can not add it to my Viewport3D How I can use those 3D objects from .xaml files in my Viewport3D?
I tryed to read xaml with XamlReader cast it to viewport3D and then take vieport3D child elements and add to my viewport, but I get error "Specified 开发者_运维百科Visual is already a child of another Visual or the root of a CompositionTarget."
Try the code below. I haven't really worked with 3D in WPF but I'm guessing it behaves the same as the common WPF Panels:
Viewport3D vp = XamlReader.Load(fileName) as Viewport3D;
//Move the child visuals to a temporary collection.
List<Visual3D> items = new List<Visual3D>();
foreach (Visual3D visual in vp.Children)
{
items.Add(visual);
}
//"Detach" the visuals from the original parent.
vp.Children.Clear();
//Now, put it in the destination viewport.
foreach (var item in items)
{
myViewport.Children.Add(item);
}
精彩评论