[咱也玩一玩]WPF立方体盒子承载DataGrid可旋转可平移可编辑

lizhibin11 2012-07-18 10:55:58
加精
最近在论坛看到几个WPF和XNA的三维问题,在尝试回答时研究了一下,现炒现卖,做了一个立方体盒子承载DataGrid,可以用方向键上下左右旋转,用鼠标滚轮前进后退,DataGrid可以编辑。

代码:
UserControl

<UserControl x:Class="WpfApplication2.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<DataGrid Grid.Row="1" Name="dataGrid1" RowBackground="Orange" ColumnWidth="*" />
<DataGrid Grid.Column="1" Grid.Row="1" Name="dataGrid2" RowBackground="Yellow" ColumnWidth="*" />
<DataGrid Grid.Column="2" Grid.Row="1" Name="dataGrid3" RowBackground="Red" ColumnWidth="*" />
<DataGrid Grid.Column="3" Grid.Row="1" Name="dataGrid4" RowBackground="Blue" ColumnWidth="*" />
<DataGrid Grid.Column="1" Name="dataGrid5" RowBackground="Green" ColumnWidth="*" />
<DataGrid Grid.Column="1" Grid.Row="2" Name="dataGrid6" RowBackground="White" ColumnWidth="*" />
</Grid>
</UserControl>

MainWindow

<Window xmlns:my="clr-namespace:WpfApplication2" x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="600" Width="800" Loaded="Window_Loaded">
<Grid Name="grid1">
<Viewport3D>
<Viewport3D.Camera>
<PerspectiveCamera Position="0, 0, 0" FarPlaneDistance="2" LookDirection="0, 0, -1" NearPlaneDistance="0.1" UpDirection="0 ,1, 0" FieldOfView="135" />
</Viewport3D.Camera>
<Viewport2DVisual3D x:Name="v23">
<Viewport2DVisual3D.Geometry>
<MeshGeometry3D Positions="-1, 1, 1 -1, -1, 1 -1, 1, -1 -1, -1, -1 1, 1, -1 1, -1, -1 1, 1, 1
1, -1, 1 -1, 1, 1 -1, -1, 1 -1, 1, 1 1, 1, 1 -1, -1, 1 1, -1, 1"
TextureCoordinates="0, 0.3333 0, 0.6666 0.25, 0.3333 0.25, 0.6666 0.5,0.3333 0.5, 0.6666
0.75, 0.3333 0.75, 0.6666 1, 0.3333 1, 0.6666 0.25, 0 0.5, 0 0.25, 1 0.5, 1"
TriangleIndices="0 1 3 0 3 2 2 3 5 2 5 4 4 5 7 4 7 6 6 7 9 6 9 8 10 2 4 10 4 11 3 12 13 3 13 5"/>
</Viewport2DVisual3D.Geometry>
<Viewport2DVisual3D.Material>
<DiffuseMaterial Viewport2DVisual3D.IsVisualHostMaterial="True" Brush="White" />
</Viewport2DVisual3D.Material>
<my:UserControl1 x:Name="ucontrol" Width="800" Height="600" />
</Viewport2DVisual3D>
<ModelVisual3D>
<ModelVisual3D.Content>
<Model3DGroup>
<DirectionalLight Color="#FFFFFFFF" Direction="0, 0, -1" />
<DirectionalLight Color="#FFFFFFFF" Direction="-1, 0, 0" />
<DirectionalLight Color="#FFFFFFFF" Direction="1, 0, 0" />
<DirectionalLight Color="#FFFFFFFF" Direction="0, 1, 0" />
<DirectionalLight Color="#FFFFFFFF" Direction="0, -1, 0" />
</Model3DGroup>
</ModelVisual3D.Content>
</ModelVisual3D>
</Viewport3D>
</Grid>
</Window>

MainWindow.cs

namespace WpfApplication2
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
RotateTransform3D rt3dud;
RotateTransform3D rt3dlr;
TranslateTransform3D tl3d;
double updown = 0;
double leftright = 0;
int translate = 0;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
DataGrid[] dgs = { ucontrol.dataGrid1, ucontrol.dataGrid2, ucontrol.dataGrid3,
ucontrol.dataGrid4, ucontrol.dataGrid5, ucontrol.dataGrid6 };
for (int i = 0; i < 6; i++)
{
DataTable dt = new DataTable();
for (int j = 0; j < 4; j++)
dt.Columns.Add(((char)(4 * i + j + 65)).ToString(), typeof(int));
for (int m = 0; m < 20; m++)
dt.Rows.Add(m, m, m, m);
dgs[i].ItemsSource = dt.DefaultView;
}
rt3dud = new RotateTransform3D(AxisAngleRotation3D.Identity);
rt3dlr = new RotateTransform3D(AxisAngleRotation3D.Identity);
tl3d = new TranslateTransform3D(new Vector3D(0, 0, 0));
Transform3DGroup t3dg = new Transform3DGroup();
t3dg.Children.Add(rt3dud);
t3dg.Children.Add(rt3dlr);
t3dg.Children.Add(tl3d);
v23.Transform = t3dg;
}
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
switch (e.Key)
{
case Key.Up:
updown -= 5;
rt3dud.Rotation = new AxisAngleRotation3D(new Vector3D(1, 0, 0), updown);
break;
case Key.Down:
updown += 5;
rt3dud.Rotation = new AxisAngleRotation3D(new Vector3D(1, 0, 0), updown);
break;
case Key.Left:
leftright -= 5;
rt3dlr.Rotation = new AxisAngleRotation3D(new Vector3D(0, 1, 0), leftright);
break;
case Key.Right:
leftright += 5;
rt3dlr.Rotation = new AxisAngleRotation3D(new Vector3D(0, 1, 0), leftright);
break;
default:
return;
}
}
protected override void OnPreviewMouseWheel(MouseWheelEventArgs e)
{
e.Handled = true;
tl3d.OffsetZ = e.Delta< 0 ? --translate * 0.05 : ++translate * 0.05;
}
}
}
...全文
3827 71 打赏 收藏 转发到动态 举报
写回复
用AI写文章
71 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_27043227 2015-04-03
  • 打赏
  • 举报
回复
好屌的样子……
w单行者 2015-02-09
  • 打赏
  • 举报
回复
LZ有源码吗,可以参考下吗
Morrisss_ 2013-08-17
  • 打赏
  • 举报
回复
支持一下!!!
coolcalf 2013-08-15
  • 打赏
  • 举报
回复
我认为wpf在桌面应用来说,所向无敌. html5与wpf根本不是在同一个战场.
danniejen 2012-08-31
  • 打赏
  • 举报
回复
弄下来跑了一圈,感觉速度好慢啊,优化下吧
yijianwang2012 2012-08-03
  • 打赏
  • 举报
回复
刚刚注册来这里 迷迷糊糊 蒙头转向 好像不是什么好地方
LAONINGA098 2012-08-01
  • 打赏
  • 举报
回复
支持分享
Jarno_zhang 2012-07-31
  • 打赏
  • 举报
回复
[Quote=引用 61 楼 的回复:]

学习了,顶楼主,请问大家谁知道wpf中的3d模型有什么工具能帮忙做不?靠3角形自己拼太难了吧?正方形还行,稍微复杂一点的怎么办?
[/Quote]

AutoDesk 3Dmax
wubaowang 2012-07-28
  • 打赏
  • 举报
回复
[Quote=引用 45 楼 的回复:]

支持下哦~
[/Quote]

......
Keep_Silence_ 2012-07-26
  • 打赏
  • 举报
回复
看起来和厉害的样子,Mark下。
orc536 2012-07-25
  • 打赏
  • 举报
回复
学习了,顶楼主,请问大家谁知道wpf中的3d模型有什么工具能帮忙做不?靠3角形自己拼太难了吧?正方形还行,稍微复杂一点的怎么办?
danxuezx 2012-07-24
  • 打赏
  • 举报
回复
厉害,要好好学习一下
Louis-Lv 2012-07-24
  • 打赏
  • 举报
回复
华丽丽的没看懂啊!!!
Louis-Lv 2012-07-24
  • 打赏
  • 举报
回复
华丽丽的没看懂啊!!!
  • 打赏
  • 举报
回复
虽然不太明白,看起来很厉害的样子。
狩护 2012-07-23
  • 打赏
  • 举报
回复
虽然不懂这方面的技术, 不过还是 顶一下啦
newtee 2012-07-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

虽然不太明白,看起来很厉害的样子。
[/Quote] 还是野比厉害啊
b8820230 2012-07-23
  • 打赏
  • 举报
回复
华丽丽的看不懂
风尘浪客 2012-07-21
  • 打赏
  • 举报
回复
这是啥东东,没见过~
yangsuai66 2012-07-21
  • 打赏
  • 举报
回复
很有用
加载更多回复(33)

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧