如何以编程方式设置附加属性,例如。 Viewport2DVisual3D.IsVisualHostMaterialProperty

时间:2014-07-04 09:52:14

标签: c# wpf dependency-properties attached-properties

我想知道如何以编程方式设置WPF依赖属性Viewport2DVisual3D.IsVisualHostMaterialProperty。

在xaml中我会使用:

<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>

但是如何才能在后面的代码中完成?

1 个答案:

答案 0 :(得分:7)

这很简单

只需给DiffuseMaterial一个名称

<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>

代码

像这样设置

diffuse.SetValue(Viewport2DVisual3D.IsVisualHostMaterialProperty, true);

Viewport2DVisual3D.SetIsVisualHostMaterial(diffuse, true);

属性Viewport2DVisual3D.IsVisualHostMaterialProperty是附加属性,可以按上述方式设置