如何在 WPF 中移动按钮

发布于 2024-11-18 08:51:29 字数 2771 浏览 7 评论 0原文

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>

XAML.cs 内,

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

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

XAML code is below

<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>

this is inide XAML.cs

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

I want to move simpleButton when P is pressed from keyboard, but I can't seem to find any method or way to do that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

脸赞 2024-11-25 08:51:29

如果不需要动画,只需执行

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

left、top、right、bottom 应该是数字

,并且您已将 keydown event 添加到按钮,因此按钮必须聚焦才能接收 < code>keydown 事件

还请查看 WPF 中的 TranslateTransform

if animation is not required just do

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

left,top,right,bottom should be numbers

and you have added keydown event to button so button must be focused in order to receive the keydown event

also look at TranslateTransform in WPF

浪推晚风 2024-11-25 08:51:29

要在网格内移动按钮,您必须处理 MainWindow 的 keydown 事件,请参见以下示例:

       private void Grid_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.P)
        {
            button1.Margin = new Thickness(2, 2, 2, 2);
        }
    }

此外,如果您还没有解释活动表单上的焦点 KeyDown 事件将如何不被
回忆道。

再见

to move the button within the grid you have to handle the keydown event of the MainWindow, see following example:

       private void Grid_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.P)
        {
            button1.Margin = new Thickness(2, 2, 2, 2);
        }
    }

Also if you have not explained how the focus on the active form KeyDown event will not be
recalled.

Bye

很快妥协 2024-11-25 08:51:29

您可以通过多种方式移动它。您可以将其放在 Canvas 控件上并更改其 Canvas.Left 或 Canvas.Top。您可以将其放入 Grid 中并更改其 Grid.Row 或 Grid.Column。也许最灵活的方法是对其应用 TranslateTransform,这也会将其移动您指定的量。

You can move it many ways. You could put it on a Canvas control and change its Canvas.Left or Canvas.Top. You could put it inside a Grid and change its Grid.Row or Grid.Column. Probably the most flexible way is to apply a TranslateTransform to it, which will also move it the amount you specify.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文