Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use databinding for 3D elements like Visual3D or UIElement3D

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>

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

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.

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.

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>

How can I create the above binding from code, when I don't have access to FrameworkElement.SetBinding?

like image 507
Isak Savo Avatar asked Oct 11 '11 11:10

Isak Savo


1 Answers

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

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);
like image 152
Isak Savo Avatar answered Sep 20 '22 13:09

Isak Savo