ViewPort3D:如何使用后面的代码创建带有文本的WPF对象(多维数据集) [英] ViewPort3D: How to create a WPF Object (Cube) with text on it from Code behind
问题描述
我想绘制一组3D多维数据集,每个多维数据集都应显示一个名称,并且在选择该多维数据集时也应具有自己的事件处理程序.
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.
是否可以使用背后的代码或xaml绑定实现它?
Is it possible to implement it using code behind or xaml binding?
推荐答案
要从背后的代码绘制3D立方体,我将使用Helix3D工具箱CubeVisual3D.但是,如果您要坚持使用现有的WPF 3D元素,则实现起来非常简单.
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环境中的文本 http://www.codeproject.com/Articles/33893/WPF-Creation-of-Text-Labels-for-3D-Scene ,它将引导您通过两种不同的方法将文本添加到3D图像中,我发现乐于助人.
start here to Learn about Text in 3D enviroments http://www.codeproject.com/Articles/33893/WPF-Creation-of-Text-Labels-for-3D-Scene which will guide you through two different approaches to adding Text to a 3D image and I find very helpful.
对于多维数据集,只需使用RectangleVisual3D对象即可.
For a cube Just use a RectangleVisual3D object Something like this.
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.
也添加事件处理程序,我相信您必须将处理程序添加到Viewport3D.这种性质的东西应该起作用.
Too add the event handler I believe you must add the Handler to the Viewport3D. Something of this nature should work.
public Window1()
{
InitializeComponent();
this.mainViewport.MouseDown += new MouseButtonEventHandler(mainViewport_MouseDown);
this.mainViewport.MouseUp += new MouseButtonEventHandler(mainViewport_MouseUp);
}
然后添加此功能
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
}
这篇关于ViewPort3D:如何使用后面的代码创建带有文本的WPF对象(多维数据集)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!