How to set an attached property programmatically eg. Viewport2DVisual3D.IsVisualHostMaterialProperty
Asked Answered
R

1

5

I'd like to know how to programmatically set the WPF Dependecy Property Viewport2DVisual3D.IsVisualHostMaterialProperty .

In the xaml I would use:

<Viewport2DVisual3D>
    <Viewport2DVisual3D.Geometry>
        <MeshGeometry3D Positions = "0,0,0 0,-30.9274,0 0,-30.9274,-24.4287 0,0,-24.4287"
                        TextureCoordinates = "0,0 0,1 1,1 1,0"
                        TriangleIndices = "0 1 2 0 2 3"/>
    </Viewport2DVisual3D.Geometry>
    <Viewport2DVisual3D.Material>
        <DiffuseMaterial Viewport2DVisual3D.IsVisualHostMaterial="True"/>
    </Viewport2DVisual3D.Material>

    <Viewport2DVisual3D.Visual>
        <Grid>
            <Image Source="{StaticResource BG}"/>
        </Grid>
    </Viewport2DVisual3D.Visual>
</Viewport2DVisual3D>

But how can it be done in code behind?

Reduplication answered 4/7, 2014 at 9:52 Comment(0)
H
9

It's fairly simple

just give the DiffuseMaterial a name

<Viewport2DVisual3D>
    <Viewport2DVisual3D.Geometry>
        <MeshGeometry3D Positions="0,0,0 0,-30.9274,0 0,-30.9274,-24.4287 0,0,-24.4287"
                        TextureCoordinates="0,0 0,1 1,1 1,0"
                        TriangleIndices="0 1 2 0 2 3" />
    </Viewport2DVisual3D.Geometry>
    <Viewport2DVisual3D.Material>
        <DiffuseMaterial x:Name="diffuse" />
    </Viewport2DVisual3D.Material>

    <Viewport2DVisual3D.Visual>
        <Grid>
            <Image Source="{StaticResource BG}" />
        </Grid>
    </Viewport2DVisual3D.Visual>
</Viewport2DVisual3D>

in code

set it like this

diffuse.SetValue(Viewport2DVisual3D.IsVisualHostMaterialProperty, true);

or

Viewport2DVisual3D.SetIsVisualHostMaterial(diffuse, true);

the property Viewport2DVisual3D.IsVisualHostMaterialProperty is an attached property which can be set in the above mentioned ways

Hilten answered 4/7, 2014 at 9:55 Comment(1)
Thanks, I needed to add a set of Viewport2DVisual3D objects at runtime so I "translated" the other instructions in C# but couldn't find out how to set that particoular property...Reduplication

© 2022 - 2024 — McMap. All rights reserved.