I'm trying to create a 3d scene to represent graphically my model. My model contains N objects (objA, objB, ...) each of them derive from an abstract class that has some property like Position,With,Height,Length etc
At th开发者_开发知识库e moment my Viewport3D is done in this way:
<Viewport3D>
...
<ModelVisual3D Content="{StaticResource objAView}">
<ModelVisual3D.Transform>
<Transform3DGroup>
<ScaleTransform3D ScaleX="{Binding Path=objA.Width}" ScaleY="{Binding Path=objA.Height}" ScaleZ="{Binding Path=objA.Length}"/>
<TranslateTransform3D OffsetX="{Binding Path=objA.Position.X}" OffsetY="{Binding Path=objA.Position.Y}" OffsetZ="{Binding Path=objA.Position.Z}"/>
</Transform3DGroup>
</ModelVisual3D.Transform>
</ModelVisual3D>
<ModelVisual3D Content="{StaticResource objBView}">
<ModelVisual3D.Transform>
<Transform3DGroup>
<ScaleTransform3D ScaleX="{Binding Path=objB.Width}" ScaleY="{Binding Path=objB.Height}" ScaleZ="{Binding Path=objB.Length}"/>
<TranslateTransform3D OffsetX="{Binding Path=objB.Position.X}" OffsetY="{Binding Path=objB.Position.Y}" OffsetZ="{Binding Path=objB.Position.Z}"/>
</Transform3DGroup>
</ModelVisual3D.Transform>
</ModelVisual3D>
...
</Viewport3D>
Is it possible to avoid to rewrite the Transform3DGroup for each ModelVisual3D giving that they are almost the same?
Thanks
I don't think that there's a way to do it with resources the way your title suggests, because you'd only be able to specify the binding once.
However, you could write a class that derives from Transform3DGroup
and takes a single binding of whatever the objA and obB are. So you could have something like this in your code instead:
<Viewport3D>
...
<ModelVisual3D Content="{StaticResource objAView}">
<ModelVisual3D.Transform>
<custom:MyTransform3DGroup ObjectToBindAgainst="{Binding objA}" />
</ModelVisual3D.Transform>
</ModelVisual3D>
<ModelVisual3D Content="{StaticResource objBView}">
<ModelVisual3D.Transform>
<custom:MyTransform3DGroup ObjectToBindAgainst="{Binding objB}" />
</ModelVisual3D.Transform>
</ModelVisual3D>
...
</Viewport3D>
And then in your custom class you can use the ObjectToBindAgainst to set all the other pieces of the included transforms.
It doesn't save you a whole lot if you're really just doing this twice, but if you're doing it many times, it might be worth it.
精彩评论