日期:2014-05-18  浏览次数:21167 次

[咱也玩一玩]WPF立方体盒子承载DataGrid可旋转可平移可编辑
最近在论坛看到几个WPF和XNA的三维问题,在尝试回答时研究了一下,现炒现卖,做了一个立方体盒子承载DataGrid,可以用方向键上下左右旋转,用鼠标滚轮前进后退,DataGrid可以编辑。

代码:
UserControl
XML code

<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
XML code

<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"/>
                </Vi