Using of 3DTools inside of UserControl

Topics: Developer Forum, User Forum
Feb 25, 2007 at 9:57 AM
Edited Feb 25, 2007 at 9:57 AM
Hello,

I inspired by your Channel9Demo and tried to create my UserControl. It would be a ButtonWithTwoFaces.

What is a problem? It looks, that 3D tranformations I've defined, are not applied, because when I build and run it, there is only second button visible (it overlaps the first one). When I comment out the second one (and its material, transformation and so on), first button is shown correctly. When I set up my dependency property AngleOfRotation to for example 30 degrees, it does not affect the viewport.

BUT: If i test my xaml and codebehind not as UserControl, but directly inside of Window, it works correctly...

Is there any limitation of using 3DTools in a UserControl? Or is there any aother problem?

Thank you.

RostaB

Here is my XAML:

<UserControl x:Class="Channel9Demo.ButtonWithTwoFaces"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:_3DTools;assembly=3DTools"
xmlns:cvrt="clr-namespace:Channel9Demo"
xmlns:own="clr-namespace:Channel9Demo"
>

<UserControl.Resources>

<cvrt:ArithmeticConvertor x:Key="arithConvertor" />

<!-- Visuals -->

<Button x:Key="Visual1" Width="120" Height="90" Click="ButtonClick" Content="First face">
</Button>

<Button x:Key="Visual2" Width="120" Height="90" Click="ButtonClick" Content="Second face">
</Button>

<!-- 3D Transforms-->
<Transform3DGroup x:Key="Transform1" x:Name="Transform1">
<RotateTransform3D>
<RotateTransform3D.Rotation>
<AxisAngleRotation3D Angle="{Binding ElementName=MyDecorator, Path=AngleOfRotation}" Axis="0 1 0" />
</RotateTransform3D.Rotation>
</RotateTransform3D>
</Transform3DGroup>

<Transform3DGroup x:Key="Transform2">
<RotateTransform3D>
<RotateTransform3D.Rotation>
<AxisAngleRotation3D Axis="0 1 0">
<AxisAngleRotation3D.Angle>
<Binding ElementName="MyDecorator" Path="AngleOfRotation" Converter="{StaticResource arithConvertor}" ConverterParameter="-180"/>
</AxisAngleRotation3D.Angle>
</AxisAngleRotation3D>
</RotateTransform3D.Rotation>
</RotateTransform3D>
</Transform3DGroup>

<!-- 3D Models -->
<MeshGeometry3D x:Key="PlaneMesh" Positions="-4,3,0 -4,-3,0 4,-3,0 4,3,0" TextureCoordinates="0,0 0,1 1,1 1,0" TriangleIndices="0 1 2 0 2 3"/>

<!-- Materials -->
<DiffuseMaterial x:Key="Material1">
<DiffuseMaterial.Brush>
<VisualBrush Visual="{StaticResource Visual1}"/>
</DiffuseMaterial.Brush>
</DiffuseMaterial>

<DiffuseMaterial x:Key="Material2">
<DiffuseMaterial.Brush>
<VisualBrush Visual="{StaticResource Visual2}"/>
</DiffuseMaterial.Brush>
</DiffuseMaterial>

<!--Decorator styler-->

<Style TargetType="local:Interactive3DDecorator" x:Key="3DDecoratorStyler">
<Setter Property="own:OwnPropertiesOfInteractive3DDecorator.AngleOfRotation"
Value="0"/>
</Style>

</UserControl.Resources>


<local:Interactive3DDecorator Name="MyDecorator" Style="{StaticResource 3DDecoratorStyler}">
<Viewport3D Width="240" Height="180">

<Viewport3D.Camera>
<PerspectiveCamera FieldOfView="90" Position="0, 0, 4" LookDirection="0, 0, -1"/>
</Viewport3D.Camera>

<local:InteractiveVisual3D Transform="{StaticResource Transform1}"
Geometry="{StaticResource PlaneMesh}"
Visual="{StaticResource Visual1}" />

<local:InteractiveVisual3D Transform="{StaticResource Transform2}"
Geometry="{StaticResource PlaneMesh}"
Visual="{StaticResource Visual2}" />

<ModelVisual3D>
<ModelVisual3D.Content>
<DirectionalLight Color="#FFFFFFFF" Direction="0,-1,-2"/>
</ModelVisual3D.Content>
</ModelVisual3D>
</Viewport3D>
</local:Interactive3DDecorator>
</UserControl>



And here is definition of dependency property:

public class OwnPropertiesOfInteractive3DDecorator
: _3DTools.Interactive3DDecorator
{

public OwnPropertiesOfInteractive3DDecorator()
: base()
{
}

static OwnPropertiesOfInteractive3DDecorator()
{
try
{
AngleOfRotationProperty = DependencyProperty.Register("AngleOfRotation", typeof(Double), typeof(_3DTools.Interactive3DDecorator), new FrameworkPropertyMetadata(0d, FrameworkPropertyMetadataOptions.AffectsRender));
}
catch (ArgumentException)
{
}
}

public Double AngleOfRotation
{
get { return (Double)this.GetValue(AngleOfRotationProperty); }
set { this.SetValue(AngleOfRotationProperty, value); }
}

public static readonly DependencyProperty AngleOfRotationProperty;
}

Coordinator
Mar 20, 2007 at 8:41 PM
Have you been able to get this working? I just tried out something myself, and it seemed like UserControls were ok. I didn't have your full source, so I had to comment out your dependency property and converter code, so I'm not sure if those are causing problems. Are you sure you're setting all of these values up correctly - I imagine it could be an issue with something not being correct with the converter or dependency property.