Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set an attached property programmatically eg. Viewport2DVisual3D.IsVisualHostMaterialProperty

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?

like image 588
Marco Avatar asked Jul 04 '14 09:07

Marco


People also ask

How to create attached properties WPF?

How to create an attached property. If your class defines an attached property solely for use by other types, then your class doesn't need to derive from DependencyObject. Otherwise, follow the WPF model of having an attached property also be a dependency property, by deriving your class from DependencyObject .

What is an attached property?

Attached property is property that has been seized pursuant to a court order, either as a provisional pre-judgment remedy or for the enforcement of a final judgment. Property may be attached only after the commencement of a lawsuit.

What is attached property in WPF with example?

Attached properties are properties which can be set on any wpf object (basically, at least a DependencyObject) via the DependencyObject. SetValue method. The purpose for this mechanism is to "attach" to other objects information needed by parent objects, not the child objects themselves. For example, the Grid.


1 Answers

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

like image 155
pushpraj Avatar answered Oct 14 '22 01:10

pushpraj