c# - 如何在WPF中 move 按钮

标签 c# wpf xaml button move

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>


这是inide XAML.cs

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


我想从键盘上按下P时移动simpleButton,但是我似乎找不到任何方法或方式来实现。

最佳答案

如果不需要动画就做

if (e.Key == Key.P)
{
   cubeButton.Margin = new Thickness(60, 50, 50, 60);
}


左,上,右,底应为数字

并且您已经在按钮中添加了keydown event,因此必须使按钮处于焦点状态才能接收keydown事件

也看TranslateTransform in WPF

关于c# - 如何在WPF中 move 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6542222/

相关文章:

c# - 如何将 FluentValidation 库与 MediatR IPipelineBehavior 一起使用?

c# - Kinect 中的手形指针

c# - 如何在代码后面将 Line.StrokeThickness 属性与 Slider 值绑定(bind)?

wpf - XAML IsMouseOver 和 IsEnabled 的奇怪冲突导致按钮在禁用时显示为事件状态

c# - DataTemplate 中带有 DoubleAnimation 的 Storyboard

c# - 在 Swagger 中显示不同的 Controller 名称

c# - 从 C# 枚举到函数/对象名称的 Json 序列化

c# - 将 3D 点投影到 2D 屏幕坐标

c# - 如何将 'hermite' 曲线转换为贝塞尔曲线?

c# - WPF DataGrid 左上角选择所有标题按钮不聚焦网格