ViewPort3D:如何从背后的代码创建一个带有文本的 WPF 对象(立方体)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11615519/
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 04:40:54  来源:igfitidea点击:

ViewPort3D: How to create a WPF Object (Cube) with text on it from Code behind

c#wpfwpf-controlsviewport3dhelix-3d-toolkit

提问by Asmaa Edress

I want to draw a set of 3D-Cubes, each Cube should display a name and also should have its own event handler when the cube is selected.

我想绘制一组 3D 立方体,每个立方体都应该显示一个名称,并且在选择立方体时还应该有自己的事件处理程序。

Is it possible to implement it using code behind or xaml binding?

是否可以使用代码隐藏或 xaml 绑定来实现它?

回答by Nomad101

To draw a 3D cube from code behind I would use the Helix3D toolkit CubeVisual3D. However if you want to stick with stock WPF 3D elements it is fairly simple to implement.

要从背后的代码绘制 3D 立方体,我将使用 Helix3D 工具包 CubeVisual3D。但是,如果您想坚持使用库存 WPF 3D 元素,则实现起来相当简单。

start here to Learn about Text in 3D enviroments http://www.codeproject.com/Articles/33893/WPF-Creation-of-Text-Labels-for-3D-Scenewhich will guide you through two different approaches to adding Text to a 3D image and I find very helpful.

从这里开始了解 3D 环境中的文本http://www.codeproject.com/Articles/33893/WPF-Creation-of-Text-Labels-for-3D-Scene它将指导您通过两种不同的方法将文本添加到一个 3D 图像,我觉得很有帮助。

For a cube Just use a RectangleVisual3D object Something like this.

对于立方体,只需使用 RectangleVisual3D 对象就可以了。

    RectangleVisual3D myCube = new RectangleVisual3D();
    myCube.Origin = new Point3D(0, 0, 0); //Set this value to whatever you want your Cube Origen to be.
    myCube.Width = 5; //whatever width you would like.
    myCube.Length = 5; //Set Length = Width
    myCube.Normal = new Vector3D(0, 1, 0); // if you want a cube that is not at some angle then use a vector in the direction of an axis such as this one or <1,0,0> and <0,0,1>
    myCube.LengthDirection = new Vector3D(0, 1, 0); //This will depend on the orientation of the cube however since it is equilateral just set it to the same thing as normal.
    myCube.Material = new DiffuseMaterial(Brushes.Red); // Set this with whatever you want or just set the myCube.Fill Property with a brush type.

Too add the event handler I believe you must add the Handler to the Viewport3D. Something of this nature should work.

太添加事件处理程序我相信您必须将处理程序添加到 Viewport3D。这种性质的东西应该起作用。

    public Window1()
    {
    InitializeComponent();
    this.mainViewport.MouseDown += new MouseButtonEventHandler(mainViewport_MouseDown);
    this.mainViewport.MouseUp += new MouseButtonEventHandler(mainViewport_MouseUp);
    }

Then Add this function

然后添加这个功能

    ModelVisual3D GetHitResult(Point location)
    {
    HitTestResult result = VisualTreeHelper.HitTest(mainViewport, location);
    if(result != null && result.VisualHit is ModelVisual3D)
    {
    ModelVisual3D visual = (ModelVisual3D)result.VisualHit;
    return visual;
    }

    return null;
    }

Then Add in the Event Handlers

然后添加事件处理程序

    void mainViewport_MouseUp(object sender, MouseButtonEventArgs e)
    {
    Point location = e.GetPosition(mainViewport);
    ModelVisual3D result = GetHitResult(location);
    if(result == null)
    {
    return;
    }
    //Do Stuff Here
    }

    void mainViewport_MouseDown(object sender, MouseButtonEventArgs e)
    {
    Point location = e.GetPosition(mainViewport);
    ModelVisual3D result = GetHitResult(location);
    if(result == null)
    {
    return;
    }
    //Do Stuff Here
    }