如何对3D元素(如Visual3D或UIElement3D)使用数据绑定 [英] How can I use databinding for 3D elements like Visual3D or UIElement3D

查看:114
本文介绍了如何对3D元素(如Visual3D或UIElement3D)使用数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的视图模型中有很多项目,我将使用Viewport3D进行3D渲染。 UI的相关部分定义如下:

I have a bunch of items in my viewmodel that I'm going to render in 3D using a Viewport3D. The relevant pieces of the UI is defined as such:

<Viewport3D x:Name="viewport">
    <Viewport3D.Children>
        <!-- This is my custom control, deriving from UIElement3D -->
        <local:myCollectionVisualizer Items="{Binding MyItems}" />
    </Viewport3D.Children>
</Viewport3D>

(我从上面的xaml中省略了光和照相机,但是我的代码中当然也有)

(I omitted light and camera from the xaml above but of course that's also there in my code)

现在我想让我的自定义控件呈现所有项目(来自我定义的 Items 依赖项属性)作为单独的 UIElement3D 元素放置在其绑定对象指定的位置。

Now I would like my custom control to render all the items (from the Items dependency property that I've defined) as individual UIElement3D elements positioned at the location specified by their bound object.

到目前为止,我的方法是覆盖 GetVisual3DChild 和相关方法/属性来为我的商品设置父子关系。我面临的问题是如何将孩子的翻译转换数据绑定到绑定对象中定义的值上-由于UIElement3D并非从FrameworkElement派生,因此没有SetBinding方法,因此也没有明显的方法从代码中指定绑定。

My approach so far is to override the GetVisual3DChild and related methods/properties to setup the parent-child relationship for my items. The problem I'm facing is how to data bind the child's translation transform to the values defined in my bound object - Since UIElement3D doesn't derive from FrameworkElement, there is no SetBinding method and thus no apparent way to specify the binding from code.

请注意,绑定转换在XAML中工作正常:

Note that binding the transform works fine in XAML:

<Viewport3D x:Name="viewport">
    <Viewport3D.Children>
        <perspective:Spherical3D>
            <perspective:Spherical3D.Transform>
                <Transform3DGroup>
                    <!-- This works fine! -->
                    <TranslateTransform3D OffsetX="{Binding X}" 
                                          OffsetY="{Binding Y}" 
                                          OffsetZ="{Binding Z}" />
                </Transform3DGroup>
            </perspective:Spherical3D.Transform>
        </perspective:Spherical3D>
    </Viewport3D.Children>
</Viewport3D>

当我无权访问 FrameworkElement.SetBinding

推荐答案

我找到了自己问题的答案,因此将其发布在这里,以供其他人稍后找到此页面使用。

I found the answer to my own question so I'm posting it here for the benefit of others finding this page later.

要在非框架元素上的依赖项属性上创建绑定,请使用静态 BindingOperations.SetBinding 方法。就我而言,最终结果是这样的:

To create a binding on a dependency property on something which isn't a framework element, you use the static BindingOperations.SetBinding method. In my case, the end result was something like this:

var visual = new Spherical3D();
var tx = new TranslateTransform3D();
BindingOperations.SetBinding(tx, TranslateTransform3D.OffsetXProperty, 
                             new Binding("X") { Source = myDataObject });
BindingOperations.SetBinding(tx, TranslateTransform3D.OffsetYProperty, 
                             new Binding("Y") { Source = myDataObject });
BindingOperations.SetBinding(tx, TranslateTransform3D.OffsetZProperty, 
                             new Binding("Z") { Source = myDataObject });
visual.Transform = tx;
Children.Add(visual);
AddVisual3DChild(visual);

这篇关于如何对3D元素(如Visual3D或UIElement3D)使用数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆