当前位置: 代码迷 >> C# >> [咱也玩一玩]WPF立方体盒子承载DataGrid可旋转可平移可编辑解决方案
  详细解决方案

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

热度:36   发布时间:2016-05-05 04:48:02.0
[咱也玩一玩]WPF立方体盒子承载DataGrid可旋转可平移可编辑
本帖最后由 lizhibin11 于 2012-07-18 23:06:24 编辑
最近在论坛看到几个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;
        }
    }
}

------解决思路----------------------
我个人感觉动Camera会简单一些
------解决思路----------------------
虽然不太明白,看起来很厉害的样子。
------解决思路----------------------
看起来就很厉害
------解决思路----------------------
推荐一下
------解决思路----------------------
支持原创,支持分享
------解决思路----------------------
现在还是菜鸟,2D都还不熟,3D后面慢慢研究,留种
------解决思路----------------------
挺好的,实践一下,看看效果

------解决思路----------------------
text editor
1
2
3
4
5
6
7
------解决思路----------------------
推荐一下
------解决思路----------------------
mark一下  回家看
------解决思路----------------------
好东西!!
------解决思路----------------------
虽然不太明白,看起来很厉害的样子。
------解决思路----------------------
不知道微软还会不会发展silverlight啊。。。。wpf命运如何啊。。。。望知情人。。。
------解决思路----------------------
 这里发言,表示您接受了CSDN社区的用户行为准则。
------解决思路----------------------
该回复于2013-06-23 12:30:12被版主删除

------解决思路----------------------
     不错!
------解决思路----------------------
还真是像模像样的啊!!!!!!!!!虽然看不懂
------解决思路----------------------
该回复于2014-10-04 09:46:25被版主删除

------解决思路----------------------
很好。LZ威武
------解决思路----------------------
做个记号 以后来看
------解决思路----------------------
我觉得wpf没前途,坐等html5
------解决思路----------------------
看起来很厉害的样子
------解决思路----------------------
好牛B~~~~
------解决思路----------------------
支持一下,谢谢楼主分享。
  相关解决方案