[英]ViewPort3D: How to create a WPF Object (Cube) with text on it from Code behind
我想繪制一組3D多維數據集,每個多維數據集都應顯示一個名稱,並且在選擇多維數據集時也應具有自己的事件處理程序。
是否可以使用背后的代碼或xaml綁定實現它?
要從背后的代碼繪制3D立方體,我將使用Helix3D工具箱CubeVisual3D。 但是,如果您要堅持使用現有的WPF 3D元素,則實現起來非常簡單。
從此處開始了解3D環境中的文本http://www.codeproject.com/Articles/33893/WPF-Creation-of-Text-Labels-for-3D-Scene ,它將指導您通過兩種不同的方法將文本添加到3D圖像,我發現非常有幫助。
對於多維數據集,只需使用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.
太添加了事件處理程序,我相信您必須將處理程序添加到Viewport3D。 這種性質的東西應該起作用。
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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.