如何在 WPF 中移动按钮
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果不需要动画,只需执行
left、top、right、bottom 应该是数字
,并且您已将
keydown
event
添加到按钮,因此按钮必须聚焦才能接收 < code>keydown 事件还请查看 WPF 中的 TranslateTransform
if animation is not required just do
left,top,right,bottom should be numbers
and you have added
keydown
event
to button so button must be focused in order to receive thekeydown
eventalso look at TranslateTransform in WPF
要在网格内移动按钮,您必须处理 MainWindow 的 keydown 事件,请参见以下示例:
此外,如果您还没有解释活动表单上的焦点 KeyDown 事件将如何不被
回忆道。
再见
to move the button within the grid you have to handle the keydown event of the MainWindow, see following example:
Also if you have not explained how the focus on the active form KeyDown event will not be
recalled.
Bye
您可以通过多种方式移动它。您可以将其放在 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.