在后面的代码中设置绑定

时间:2014-10-20 13:08:53

标签: c# xaml

我想在后面用代码中的绑定旋转一个多维数据集。

我尝试了一些事情但是在这个时刻,没有工作......

我附上了源代码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")
            };


        }

    }

滑块工作核心的绑定,我想按“S”旋转立方体

1 个答案:

答案 0 :(得分: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 = (Angle + 1.0) % 360.0