WPF 调试时拖拽不生效
WPF窗体代码
<Window x:Class="SerialLabelDemo.Test.Window10"
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:SerialLabelDemo.Test"
mc:Ignorable="d" AllowDrop="True"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="35"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions> <StackPanel Grid.Row="0" Orientation="Horizontal" Background="#CC0088FF">
<Button
Name="btClear" Click="ClickClear" Content="Clear" Margin="5"
/>
<Border BorderBrush="Black" BorderThickness="1" MaxHeight="25">
<CheckBox
Name="cbWrap"
Content="Wrap Content"
IsChecked="False"
Margin="5" Padding="5,0,0,0"
VerticalAlignment="Center" VerticalContentAlignment="Center"
Click="ClickWrap"
/>
</Border>
<Label
Name="lblInstructions"
HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
FontWeight="Bold"
Content=" Drop a single file below to display its contents. "
/>
</StackPanel> <TextBox
Name="tbDisplayFileContents"
Grid.Row="1"
AcceptsReturn="True" AcceptsTab="True"
AllowDrop="True"
BorderThickness="1" BorderBrush="Black"
HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" PreviewDragOver="EhDragOver" PreviewDrop="EhDrop"/>
<TextBlock Text="管理员运行是不可以进行拖拽的"/> </Grid>
</Window>
窗体后台代码
public partial class Window10 : Window
{
public Window10()
{
InitializeComponent(); if ((bool)cbWrap.IsChecked)
tbDisplayFileContents.TextWrapping = TextWrapping.Wrap;
else
tbDisplayFileContents.TextWrapping = TextWrapping.NoWrap;
} private void ClickClear(object sender, RoutedEventArgs args)
{
tbDisplayFileContents.Clear();
} private void ClickWrap(object sender, RoutedEventArgs args)
{
if ((bool)cbWrap.IsChecked)
tbDisplayFileContents.TextWrapping = TextWrapping.Wrap;
else
tbDisplayFileContents.TextWrapping = TextWrapping.NoWrap;
} private void EhDragOver(object sender, DragEventArgs args)
{
// As an arbitrary design decision, we only want to deal with a single file.
args.Effects = IsSingleFile(args) != null ? DragDropEffects.Copy : DragDropEffects.None; // Mark the event as handled, so TextBox's native DragOver handler is not called.
args.Handled = true;
} private void EhDrop(object sender, DragEventArgs args)
{
// Mark the event as handled, so TextBox's native Drop handler is not called.
args.Handled = true; var fileName = IsSingleFile(args);
if (fileName == null) return; var fileToLoad = new StreamReader(fileName);
tbDisplayFileContents.Text = fileToLoad.ReadToEnd();
fileToLoad.Close(); // Set the window title to the loaded file.
Title = "File Loaded: " + fileName;
} // If the data object in args is a single file, this method will return the filename.
// Otherwise, it returns null.
private string IsSingleFile(DragEventArgs args)
{
// Check for files in the hovering data object.
if (args.Data.GetDataPresent(DataFormats.FileDrop, true))
{
var fileNames = args.Data.GetData(DataFormats.FileDrop, true) as string[];
// Check fo a single file or folder.
if (fileNames.Length == 1)
{
// Check for a file (a directory will return false).
if (File.Exists(fileNames[0]))
{
// At this point we know there is a single file.
return fileNames[0];
}
}
}
return null;
}
}
运行画面:

调试时发现无法拖拽:
原因,以管理员启动VS进行调试是不能拖拽外部文件到程序中的
在debug目录下以管理员运行exe运行也是一样的结果
说是UAC检测的系统上管理员权限开启的程序时无法支持外部拖拽的
同行描述原因见https://www.cnblogs.com/swack/p/10508649.html
WPF 调试时拖拽不生效的更多相关文章
- WPF 精修篇 拖拽 DragDrop
原文:WPF 精修篇 拖拽 DragDrop WPF 实现拖拽 效果 <Grid> <Grid.ColumnDefinitions> <ColumnDefinition ...
- WPF中元素拖拽的两个实例
今天结合之前做过的一些拖拽的例子来对这个方面进行一些总结,这里主要用两个例子来说明在WPF中如何使用拖拽进行操作,元素拖拽是一个常见的操作,第一个拖拽的例子是将ListBox中的子元素拖拽到ListV ...
- WPF如何实现拖拽打开文件(将文件拖进窗体打开)
在WPF中的实现和WinForm中的实现99%相似,将要实现接受拖拽释放的控件添加DragEnter事件和Drop事件,本例中控件Grid grid作为接受控件,添加事件操作如下: private v ...
- 【WPF】鼠标拖拽功能DragOver和Drop
在Winform里面实现拖入功能只要设置控件AllowDrop=true; 然后实现方法 //拖入 private void txtInputPath_DragOver(object sender, ...
- [WPF][ListBox]鼠标拖拽多选,(Shift Key、Ctrl Key多选有效)(转)
<ListBox Name="listBox" SelectionMode="Extended"> <ListBox.Resources> ...
- 【WPF/C#】拖拽Image图片控件
需求:使得Image图片控件能够被拖动. 思路:关键是重写Image控件的几个鼠标事件,实现控制. 前台: <Image Source="C:\Users\Administrator\ ...
- Qt无边框窗体-最大化时支持拖拽还原
目录 一.概述 二.效果展示 三.demo制作 1.设计窗体 2.双击放大 四.拖拽 五.相关文章 原文链接:Markdown模板 一.概述 用Qt进行开发界面时,既想要实现友好的用户交互又想界面漂亮 ...
- 理解事件捕获。在限制范围内拖拽div+吸附+事件捕获
一.实现的效果是在限制范围内拖拽div+吸附+事件捕获. 这里需要理解的是事件捕获,这个事件捕获也是为了兼容div在拖拽过程中,文本不被选中这个问题. 如此良辰美景,拖拽也可以很洒脱哈.先看看图, 二 ...
- 解决Duilib集成CEF浏览器在Win10无法向客户区拖拽文件
在Duilib中集成CEF浏览器项目实际开发中,遇到一个问题. 一个需求从资源管理器(桌面)拖拽文件到客户端,窗口捕获WM_DROPFILES消息然后进行消息处理,但客户区是集成的CEF浏览器,浏览器 ...
随机推荐
- numpy 中的堆叠 stack
参考: https://blog.csdn.net/Riverhope/article/details/78922006 vstack,vertical 垂直堆叠 hstack, horizontal ...
- js 面试题正则相关
正则相关[i不区分大小写,g匹配全部数据] var str = "Hello word! I think word is good."; 1.替换str中的word为javascr ...
- 使用 open 函数 写的代码 用户名登录
先创建文件ha.log 内容: aaa$$123bbb$$456 def dl(user,pas): f = open('ha.log', 'r', encoding="utf-8" ...
- 安卓实用工具箱v4.3几百种小功能
款多功能实用工具箱.提供了从日常.图片.查询.设备.辅助.提取.优惠券.趣味游戏等多方面的功能,操作简单,即点即用,避免您下载超多应用的难题,且应用体积轻巧,界面简洁.已去除广告! 下载地址:http ...
- Mysql常用函数合集
1. 字符函数 length(获取字节数,UTF-8编码中 一个汉字占3个字节,GBK编码中一个汉字占2个字节) select length('abc'); #结果:3 select length(' ...
- Unity 操作快捷键
Q Hand(手形)工具 可以平移整个Scene视图 W Translate(移动)工具 移动所选择的游戏对象 E Rotate(旋转)工具 按任意角度旋转游戏 ...
- 精尽Spring MVC源码分析 - HandlerExceptionResolver 组件
该系列文档是本人在学习 Spring MVC 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释 Spring MVC 源码分析 GitHub 地址 进行阅读 Spring 版本:5.2. ...
- Python实现全自动购买火车票!抢票回家过年咯
本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理这个是实现结果,因为一天只能取消三次,所以最后一步点击确认被我注释了1.首先实现使用selenium登 ...
- 利用xlutils第三方库复制excel模板
Python之利用第三方库套用excel模板,模板的样子假设如下: 现在要用这个模板,并且在"第二行第二列"的下方填入内容: #!usr/bin/python3 # -*-codi ...
- Linux下源码安装方式安装MySQL
1.下载安装包:https://downloads.mysql.com/archives/community/ 2.安装开发工具和安装包 因为要把源码编译成二进制数据,所以必须要有编译器和解释器 g ...