如何在WPF中移动按钮

Bas*_*rdo 1 c# wpf xaml move button

XAML代码如下

<Window x:Class="DenemeWpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DockPanel Width="Auto"
                   VerticalAlignment="Stretch"
                   Height="Auto"
                   HorizontalAlignment="Stretch"
                   Grid.ColumnSpan="1"
                   Grid.Column="0"
                   Grid.Row="0"
                   Margin="0,0,0,0"
                   Grid.RowSpan="1">

            <StackPanel>
                <StackPanel.Background>
                    <LinearGradientBrush>
                        <GradientStop Color="White" Offset="0" />
                        <GradientStop Color="DarkKhaki" Offset=".3" />
                        <GradientStop Color="DarkKhaki" Offset=".7" />
                        <GradientStop Color="White" Offset="1" />
                    </LinearGradientBrush>
                </StackPanel.Background>
                <StackPanel Margin="10">
                    <Button Name="simpleButton"
                            Click="simpleButtonClick"
                            KeyDown="simpleButton_KeyDown">Simple</Button>
                    <Button Name="cubeButton"
                            Click="cubeButtonClick">Cube</Button>
                </StackPanel>
            </StackPanel>
            <Viewport3D Name="mainViewport"
                        ClipToBounds="True">
                <Viewport3D.Camera>
                    <PerspectiveCamera FarPlaneDistance="100"
                                       LookDirection="-11,-10,-9"
                                       UpDirection="0,1,0"
                                       NearPlaneDistance="1"
                                       Position="11,10,9"
                                       FieldOfView="70" />
                </Viewport3D.Camera>
                <ModelVisual3D>
                    <ModelVisual3D.Content>
                        <DirectionalLight Color="White" Direction="-2,-3,-1" />
                    </ModelVisual3D.Content>
                </ModelVisual3D>
            </Viewport3D>
        </DockPanel>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

这是XAML.cs的内部

private void simpleButton_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.P) {
        //something to move the simpleButton
    }
}
Run Code Online (Sandbox Code Playgroud)

我想simpleButton从键盘按下P时移动,但我似乎无法找到任何方法或方法来做到这一点.

Afn*_*hir 6

如果不需要动画就行

if (e.Key == Key.P)
{
   cubeButton.Margin = new Thickness(60, 50, 50, 60);
}
Run Code Online (Sandbox Code Playgroud)

左,上,右,下应该是数字

并且您已添加keydown event到按钮,因此必须关注按钮才能接收keydown事件

另请参阅WPF中的TranslateTransform