2014-10-20 46 views
0

我想旋轉具有代碼後面的綁定的多維數據集。在後面的代碼中設置綁定

我嘗試了一些東西,但在這個時刻,沒有工作...

我連接的源代碼,XAML和C#

代碼XAML:

<Grid KeyDown="Grid_KeyDown_1" > 
    <Viewport3D Name="viewport3D1"> 
     <Viewport3D.Camera> 
      <PerspectiveCamera x:Name="camMain" Position="6 5 4" LookDirection="-6 -5 -4"> 
      </PerspectiveCamera> 
     </Viewport3D.Camera> 
     <ModelVisual3D> 
      <ModelVisual3D.Content> 
       <DirectionalLight x:Name="dirLightMain" Direction="-1,-1,-1"> 
       </DirectionalLight> 
      </ModelVisual3D.Content> 
     </ModelVisual3D> 
     <ModelVisual3D> 
      <ModelVisual3D.Content> 
       <GeometryModel3D> 
        <GeometryModel3D.Geometry> 
         <MeshGeometry3D x:Name="meshMain" 
          Positions="0 0 0 1 0 0 0 1 0 1 1 0 0 0 1 1 0 1 0 1 1 1 1 1" 
          TriangleIndices="2 3 1 2 1 0 7 1 3 7 5 1 6 5 7 6 4 5 6 2 0 2 0 4 2 7 3 2 6 7 0 1 5 0 5 4"> 
         </MeshGeometry3D> 
        </GeometryModel3D.Geometry> 
        <GeometryModel3D.Material> 
         <DiffuseMaterial x:Name="matDiffuseMain"> 
          <DiffuseMaterial.Brush> 
           <SolidColorBrush Color="Red"/> 
          </DiffuseMaterial.Brush> 
         </DiffuseMaterial> 
        </GeometryModel3D.Material> 
       </GeometryModel3D> 
      </ModelVisual3D.Content> 
      <ModelVisual3D.Transform> 
       <RotateTransform3D> 
        <RotateTransform3D.Rotation> 
         <AxisAngleRotation3D x:Name="rotate" Axis="0 1 0"/> 
        </RotateTransform3D.Rotation> 
       </RotateTransform3D> 
      </ModelVisual3D.Transform> 
     </ModelVisual3D> 
     </Viewport3D> 

     <Slider Grid.Row="1" Minimum="0" Maximum="360" Orientation="Horizontal" 
Value="{Binding ElementName=rotate, Path=Angle}" ></Slider> 

    </Grid> 

代碼C#:

private void Grid_KeyDown_1(object sender, KeyEventArgs e) 
    { 
     if(Keyboard.IsKeyDown(Key.S)) 
     { 
      Binding binding = new Binding { 
      Source=rotate, 
      Path=new PropertyPath("Angle") 
      }; 


     } 

    } 

滑塊工作corect的綁定,我想按「S」旋轉立方體

+0

我不認爲你明白的聯結概念。你的虛擬機應該包含一個proerpty Rotation,你的關鍵事件應該修改它。當你有綁定時,你沒有代碼隱藏。 – 2014-10-20 13:12:18

+0

我認爲你是對的 – user3094445 2014-10-20 13:18:52

回答

0

在代碼中使用依賴屬性的背後是這樣

public double Angle 
{ 
    get { return (double)GetValue(AngleProperty); } 
    set { SetValue(AngleProperty, value); } 
} 
public static readonly DependencyProperty AngleProperty = 
    DependencyProperty.Register("Angle", typeof(double), typeof(MyClass), new PropertyMetadata(0.0)); 

,然後綁定旋轉,與其。

然後在你的鍵盤處理,你可以增加角度,這樣的事情:

Angle = (Angle + 1.0) % 360.0 
相關問題