在代码隐藏中设置绑定
本文关键字:设置 绑定 隐藏 代码 | 更新日期: 2023-09-27 18:27:13
我想在代码后面旋转一个具有绑定的多维数据集。
我尝试了一些东西,但此时此刻,没有奏效。。。
我附上了源代码,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 + 1.0) % 360.0