簡體   English   中英

在代碼后面設置綁定

[英]Set binding in code behind

我想旋轉一個帶有綁定代碼的多維數據集。

我嘗試了一些嘗試,但此刻卻無法正常工作...

我附上了源代碼,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”鍵來旋轉立方體

像這樣在代碼后面使用依賴項屬性

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM