是否可以将带有绑定的 Transform3DGroup 放入资源字典中?

发布于 2024-11-07 07:43:40 字数 1434 浏览 0 评论 0原文

我正在尝试创建一个 3D 场景来以图形方式表示我的模型。 我的模型包含 N 个对象(objA,objB,...),每个对象都派生自一个抽象类,该抽象类具有一些属性,例如 Position、With、Height、Length 等

目前我的 Viewport3D 是这样完成的:

<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>

是否可以避免为每个 ModelVisual3D 重写 Transform3DGroup ,因为它们几乎相同?

谢谢

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 the 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏有森光若流苏 2024-11-14 07:43:40

我认为没有办法按照您的标题建议的方式使用资源来完成此操作,因为您只能指定一次绑定。

但是,您可以编写一个派生自 Transform3DGroup 的类,并采用 objA 和 obB 的单个绑定。因此,您可以在代码中使用类似的内容:

<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>

然后在自定义类中,您可以使用 ObjectToBindAgainst 来设置所包含转换的所有其他部分。

如果你真的只做两次,它并不能为你节省很多,但如果你做了很多次,它可能是值得的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文