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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24571504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:04:55  来源:igfitidea点击:

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

c#wpfdependency-propertiesattached-properties

提问by Marco

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

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

In the xaml I would use:

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

But how can it be done in code behind?

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

回答by pushpraj

It's fairly simple

这很简单

just give the DiffuseMaterial a name

只需为 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>

in code

在代码中

set it like this

设置成这样

diffuse.SetValue(Viewport2DVisual3D.IsVisualHostMaterialProperty, true);

or

或者

Viewport2DVisual3D.SetIsVisualHostMaterial(diffuse, true);

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

该属性Viewport2DVisual3D.IsVisualHostMaterialProperty是一个附加属性,可以通过上述方式设置