不能拖动和移动 WPF 表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1761854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cant drag and move a WPF Form
提问by Jawahar BABU
I design a WPF form with Window Style=None. So I Cannot see the drag bar in the form. How can I move the Form with WindowStyle=None Property?
我设计了一个 Window Style=None 的 WPF 表单。所以我看不到表单中的拖动条。如何使用 WindowStyle=None 属性移动表单?
回答by
I am using a main window to hold pages (creating a navigation style program), and in the code behind of my main window, I inserted this...
我正在使用主窗口来保存页面(创建导航样式程序),并且在主窗口后面的代码中,我插入了这个...
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
// Begin dragging the window
this.DragMove();
}
... and it works like a charm. This is with windowstyle=none. Its nice in the sense that you can click anywhere on the app and move it instead of just being limited to a top bar.
...它就像一个魅力。这是 windowstyle=none。从某种意义上说,您可以单击应用程序上的任意位置并移动它,而不仅限于顶部栏,这很好。
回答by Joey
See this question.
看到这个问题。
Basically you use the Window.DragMovemethod for this.
基本上,您为此使用Window.DragMove方法。
回答by TabbyCool
In our application we have Windows with WindowStyle set to "none", we implemented the functionality to drag the Window, but only from the header rather than from any point in the Window. We did this by adding a Border as a header, then adding a Thumb to fill the entire Border. We then handle the DragDelta method on the Thumb in the code-behind for the Window.
在我们的应用程序中,我们将 Windows 的 WindowStyle 设置为“none”,我们实现了拖动 Window 的功能,但仅从标题而不是从窗口中的任何点拖动。我们通过添加一个边框作为标题,然后添加一个拇指来填充整个边框来做到这一点。然后,我们在窗口的代码隐藏中处理 Thumb 上的 DragDelta 方法。
<Border
Name="headerBorder"
Width="Auto"
Height="50"
VerticalAlignment="Top"
CornerRadius="5,5,0,0"
DockPanel.Dock="Top"
Background="{StaticResource BackgroundBrush}"
BorderThickness="1,1,1,1"
BorderBrush="{StaticResource BorderBrush}">
<Grid>
<Thumb
x:Name="headerThumb"
Opacity="0"
Background="{x:Null}"
Foreground="{x:Null}"
DragDelta="headerThumb_DragDelta"/>
</Grid>
</Border>
Then in the code-behind we have the following event handler...
然后在代码隐藏中我们有以下事件处理程序......
private void headerThumb_DragDelta(object sender, DragDeltaEventArgs e)
{
Left = Left + e.HorizontalChange;
Top = Top + e.VerticalChange;
}
I don't know if this is any better than the other method, it's just the way we did it.
我不知道这是否比其他方法更好,这只是我们这样做的方式。
回答by Pranavan Maru
either inside the windows on load function or inside the grid's on load function use a deligate to trigger the DragMove() method on Mouse Click
在加载函数的窗口内或在网格的加载函数内使用 deligate 在鼠标单击时触发 DragMove() 方法
private void Grid_Loaded(object sender, RoutedEventArgs e)
{
this.MouseLeftButtonDown += delegate{DragMove();};
}
回答by Steve
If you simply add this.DragMove();
and you are using Bing Maps, then you will get some frustrating behavior when trying to pan the map.
如果您只是添加this.DragMove();
并使用 Bing 地图,则在尝试平移地图时会出现一些令人沮丧的行为。
Using TabbyCool's answerwas a good solution however, you can not drag the window against the top of the screen to maximise it.
使用TabbyCool 的答案是一个很好的解决方案,但是,您不能将窗口拖动到屏幕顶部以使其最大化。
My solution was just checking that the position.Y of my click relative to my top bar grid was less than a suitable amount.
我的解决方案只是检查我的点击相对于顶部栏网格的 position.Y 是否小于合适的数量。
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
Point pt = e.GetPosition(topBar);
Debug.WriteLine(pt.Y);
if (pt.Y < topBar.ActualHeight)
{
DragMove();
}
}