C# ViewPort3D:如何创建WPF对象(多维数据集),其中包含来自代码隐藏的文本

C# ViewPort3D:如何创建WPF对象(多维数据集),其中包含来自代码隐藏的文本,c#,wpf,wpf-controls,viewport3d,helix-3d-toolkit,C#,Wpf,Wpf Controls,Viewport3d,Helix 3d Toolkit,我想画一组3D多维数据集,每个多维数据集都应该显示一个名称,并且在选中多维数据集时应该有自己的事件处理程序 是否可以使用代码隐藏或xaml绑定来实现它?要从代码隐藏绘制3D多维数据集,我将使用Helix3D工具包CubeVisual3D。但是,如果您想坚持使用库存WPF 3D元素,那么实现起来相当简单 从这里开始学习3D环境中的文本,这将指导您通过两种不同的方法将文本添加到3D图像中,我发现这非常有用 对于立方体,只需使用矩形的Visual3D对象,如下图所示 RectangleVisu

我想画一组3D多维数据集,每个多维数据集都应该显示一个名称,并且在选中多维数据集时应该有自己的事件处理程序


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

要从代码隐藏绘制3D多维数据集,我将使用Helix3D工具包CubeVisual3D。但是,如果您想坚持使用库存WPF 3D元素,那么实现起来相当简单

从这里开始学习3D环境中的文本,这将指导您通过两种不同的方法将文本添加到3D图像中,我发现这非常有用

对于立方体,只需使用矩形的Visual3D对象,如下图所示

    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.
然后添加这个函数

    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;
    }
然后添加事件处理程序

    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
    }

你在哪方面挣扎?你会画立方体吗?如果您正在使用WPF 3D进行任何工作,还可以查看Helix 3D Toolkit()。感谢提供有关渲染问题的链接。由于我需要GeometryModel3D,您的单击解决方案对我不起作用。使用GeometryModel3D hitgeo=rayMeshResult.ModelHit作为GeometryModel3D管理并检索模型;以防有人有同样的问题:)
    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
    }