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浏览器,浏览器 ...
随机推荐
- Redis Sentinel-深入浅出原理和实战
本篇博客会简单的介绍Redis的Sentinel相关的原理,同时也会在最后的文章给出硬核的实战教程,让你在了解原理之后,能够实际上手的体验整个过程. 之前的文章聊到了Redis的主从复制,聊到了其相关 ...
- 冲刺Day7
每天举行站立式会议照片: 昨天已完成的工作: 1.确认商品分类栏,并前后端交互 2.检查.更正订单模块的代码 3.检查.更正用户模块的代码 今天计划完成的工作: 成员 任务 高嘉淳 检查代码.提供测试 ...
- caffe源码 理解链式法则
网络结构 首先我们抽象理解下一个网络结构是怎样的,如下图所示 F1,F2,F3为某种函数 input为输入数据,output为输出数据 X1,X2为为中间的层的输入输出数据 总体来说有以下关系 X1 ...
- 题解-CF1239D Catowice City
CF1239D Catowice City 有 \(n\) 个人和 \(n\) 只猫.有 \(m\) 对人猫友谊,即第 \(u_i\) 个人认识第 \(v_i\) 只猫,保证第 \(i\) 个人和第 ...
- kafka命令及启动
默认内网访问,要在外网访问的话,需要在修改config/server.properties中的配置 将listeners和advertised.listeners的值用主机名进行替换,在外用使用jav ...
- 七、Jmeter测试元件-线程
线程组元素是任何测试计划的起点.所有控制器和采样器必须在线程组下.其他元素(例如,侦听器)可以直接放置在测试计划下. 名称:相当于一个业务流程 继续:当取样器出错时 会继续请求: 举例:1-当一个线程 ...
- IDEA将Java Web项目打war包
准备工作 1.IntelliJ IDEA开发工具 2.可以正常运行的Java Web项目 打war包流程 1.使用快捷键Ctrl+Alt+Shift+s 或者 鼠标点击选中项目名按F4 打开 Proj ...
- 职场PUA,管理者的五宗罪
在目前的社会环境下,程序员似乎成了"弱势群体".我们经常谈论的职场PUA已经成为程序员的代名词. 我一直在想,为什么这么多管理者能力会这么差. 但最后最吃亏的还是可怜的程序员. 也 ...
- (30)ASP.NET Core3.1 集成Apollo快速安装与使用
1.介绍 Apollo(阿波罗)是携程框架部研发并开源的一款生产级的配置中心产品,它能够集中管理应用在不同环境.不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限.流程治理等特性,适用 ...
- 详解Python Google Protocol Buffer
为什么要使用PB? PB(Protocol Buffer)是 Google 开发的用于结构化数据交换格式,作为腾讯云日志服务标准写入格式.因此用于写入日志数据前,需要将日志原始数据序列化为 PB 数据 ...