Image.PreviewMouseLeftUp未触发(WPF)
2017-08-13 本文已影响12人
JetLu
写launcher
时遇到的一个问题。
以下结构的xaml
:
<Window x:Class="test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:test"
mc:Ignorable="d"
Background="Transparent"
WindowStyle="None"
AllowsTransparency="True"
PreviewMouseLeftButtonDown="Window_PreviewMouseLeftButtonDown"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Border Background="#80000000" CornerRadius="4">
<Grid>
<Image Source="https://cdn.lufei.so/image/avatar.png"
PreviewMouseLeftButtonUp="Image_PreviewMouseLeftButtonUp"
></Image>
</Grid>
</Border>
</Grid>
</Window>
对应的cs
:
using System.Windows.Input;
namespace test {
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
}
private void Window_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
DragMove();
Trace.WriteLine(e.OriginalSource);
}
private void Image_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) {
MessageBox.Show("image mouse up");
}
}
}
那么永远不会弹出Message box
。
查找了一些资料和自己的测试。
问题出在DragMove
。
下面这段截在 stackoverflow:
DragMove is a synchronous call; it blocks until the user is done moving the window. This means once DragMove returns, the left button is up. Add your code immediately after your DragMove() call and you should be fine.