我想知道如何以编程方式设置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>
但是如何才能在后面的代码中完成?
答案 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
是附加属性,可以按上述方式设置