[咱也玩一玩]WPF立方体盒子承载DataGrid可旋转可平移可编辑
最近在论坛看到几个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>
<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>
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; } }}