如何解决WPF-OnDrop事件块拖放到“模型”对话框中
在WPF应用程序中,我有一个MainWindow和一个Dialog。 触发MainWindow内部的OnDrop事件时,它将打开对话框。 对话框还具有一个OnDrop事件,该事件永远不会触发,因为MainWindow的OnDrop事件仍然处于活动状态,并且会阻止其他任何拖放操作。
如何从MainWindow的OnDrop事件中打开对话框,然后允许用户将数据拖放到对话框中?
MainWindow XAML
if (reaction && reaction.emoji && reaction.emoji.name){
////logic
}
MainWindow返回代码
<Window x:Class="DialogOpener.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"
mc:Ignorable="d"
x:Name="MainWindow"
Title="MainWindow" Height="450" Width="800"
AllowDrop="True">
<Label Background="Blue"
FontSize="54"
Content="Main Window" />
</Window>
对话框XAML
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
protected override void OnDrop(DragEventArgs e)
{
base.OnDrop(e);
Dialog dialog = new Dialog()
{
Owner = this
};
bool? result = dialog.ShowDialog();
if(result.HasValue && (bool)result)
{
Debug.WriteLine("Positive Result!!!");
}
e.Handled = true;
}
}
对话框后退代码
<Window x:Class="DialogOpener.Dialog"
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"
mc:Ignorable="d"
AllowDrop="True"
Title="Dialog" Height="450" Width="800">
<Label Background="Red" FontSize="54"
Content="Dialog"
AllowDrop="True" />
</Window>
解决方法
在事件处理程序完成后,使用分派器打开对话框 :
protected override void OnDrop(DragEventArgs e)
{
base.OnDrop(e);
Dispatcher.BeginInvoke(new Action(() => {
Dialog dialog = new Dialog()
{
Owner = this
};
bool? result = dialog.ShowDialog();
if (result.HasValue && (bool)result)
{
Debug.WriteLine("Positive Result!!!");
}
}));
e.Handled = true;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。