我尝试使用按钮拖动窗口,我有使用MouseDown事件拖动的代码
func mediaPickerDidCancel(_ mediaPicker: MPMediaPickerController!)
但是我想用一些按钮来做这个,因为我的表格是透明的,当我更改Button事件的代码时它不起作用
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
this.DragMove();
}
一些想法?
答案 0 :(得分:1)
使用border
代替Button。因为DragMove
仅适用于PrimaryMouseButton事件。不适用于Click事件
XAML
<Border Background="Blue" MouseLeftButtonDown="Border_MouseLeftButtonDown">
</Border>
CODE
private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DragMove();
}
答案 1 :(得分:0)
我的最后代码在这里:
private Point startPoint;
private void btnChat_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
startPoint = e.GetPosition(btnChat);
}
private void btnChat_PreviewMouseMove(object sender, MouseEventArgs e)
{
var currentPoint = e.GetPosition(btnChat);
if (e.LeftButton == MouseButtonState.Pressed &&
btnChat.IsMouseCaptured &&
(Math.Abs(currentPoint.X - startPoint.X) >
SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(currentPoint.Y - startPoint.Y) >
SystemParameters.MinimumVerticalDragDistance))
{
// Prevent Click from firing
btnChat.ReleaseMouseCapture();
DragMove();
}
}
答案 2 :(得分:0)
您必须使用PreviewMouseDown
而不是MouseDown
事件。
并且不要忘了将事件标记为“已处理”。
XAML代码:
<Button x:Name="Button_Move" PreviewMouseDown="Window_Main_MouseDown"/>
C#代码:
private void Window_Main_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == System.Windows.Input.MouseButton.Left)
{
this.DragMove();
e.Handled = true;
}
}