我设计了一个WPF窗体并设置了Window Style=None。所以我无法在窗体上看到拖动条。如何使用WindowStyle=None属性移动窗体?
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
// Begin dragging the window
this.DragMove();
}
......它的运行非常顺畅。这是使用windowstyle=none实现的。它的好处在于,您可以单击应用程序上的任何位置并将其移动,而不仅仅局限于顶部栏。
在我们的应用程序中,我们将WindowStyle设置为“none”,实现了拖动窗口的功能,但只能从标题栏拖动而不是从窗口的任何点拖动。我们通过将边框作为标题栏添加,然后添加一个Thumb来填充整个边框来实现此功能。然后,在窗口的代码后台处理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>
然后在代码后台,我们有以下事件处理程序...
private void headerThumb_DragDelta(object sender, DragDeltaEventArgs e)
{
Left = Left + e.HorizontalChange;
Top = Top + e.VerticalChange;
}
我不知道这种方法是否比其他方法更好,这只是我们的做法。
无论是在窗口的加载函数内部还是在网格的加载函数内部,都可以使用委托来触发鼠标单击时的DragMove()方法。
private void Grid_Loaded(object sender, RoutedEventArgs e)
{
this.MouseLeftButtonDown += delegate{DragMove();};
}
this.DragMove();
,并且你正在使用Bing Maps,那么在尝试平移地图时会出现一些令人沮丧的行为。使用TabbyCool的答案是一个不错的解决方案,但是你无法将窗口拖动到屏幕顶部以最大化它。我的解决方案是检查相对于我的顶部栏网格的点击位置的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();
}
}