使用helix工具包创建可单击的对象

时间:2014-12-19 15:37:22

标签: c# wpf helix-3d-toolkit

我在Helix Toolkit上找到了一个名为ScatterPlot的例子,它非常接近我真正需要的东西。但是我找不到任何关于如何向创建的对象添加onclick事件监听器的东西(在本例中为球体)。这会将球体添加到“游乐场”。

scatterMeshBuilder.AddSphere(Points[i], SphereSize, 4, 4);

基本目标是为每个球体添加一个onclick事件侦听器,当用户选择一种颜色并单击其中一个球体时,它将更改为所选颜色。可以将onclick监听器(或与之相等的东西)添加到球体中。

1 个答案:

答案 0 :(得分:4)

一年后......也许有人会觉得这很有用。

对我有用的解决方案围绕扩展UIElement3D类,这有一堆你可以覆盖的标准事件。例如MouseEnter,MouseClick等。 来源如下。

using System.Windows; 
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using System.Windows.Controls.Primitives;
using System.Windows.Controls;

public class InteractivePoint : UIElement3D   
{
    public InteractivePoint(Point3D center, Material material, double sphereSize = 0.07)
    {
       MeshBuilder builder  = new MeshBuilder();

       builder.AddSphere( center, sphereSize , 4, 4 );
       GeometryModel3D model = new GeometryModel3D( builder.ToMesh(), material );
        Visual3DModel = model;
    }

    protected override void OnMouseEnter( MouseEventArgs event )
    {
        base.OnMouseEnter( event );

        GeometryModel3D point = Visual3DModel as GeometryModel3D;

        point.Material = Materials.Red; //change mat to red on mouse enter
        Event.Handled = true;
    }

    protected override void OnMouseLeave( MouseEventArgs event )
    {
        base.OnMouseEnter( event );

        GeometryModel3D point = Visual3DModel as GeometryModel3D;

        point.Material = Materials.Blue; //change mat to blue on mouse leave
        Event.Handled = true;
    }


}

将它们添加到游乐场

Point3D[,] dataPoints = new Point3D[10,10]; // i will assume this has already been populated.
ContainerUIElement3D container;
Material defaultMaterial = Materaials.Blue;
for (int x = 0;x < 10; x++)
{
    for(int y = 0; y < 10; y++)
    {
        Point3D position = dataPoints [x, y];
        InteractivePoint  interactivePoint = new InteractivePoint( position, defaultMaterial );
        container.Children.Add( interactivePoint );
    }
}

最后将容器作为子项添加到ModelVisual3D对象。