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 调试时拖拽不生效的更多相关文章

  1. WPF 精修篇 拖拽 DragDrop

    原文:WPF 精修篇 拖拽 DragDrop WPF 实现拖拽 效果 <Grid> <Grid.ColumnDefinitions> <ColumnDefinition ...

  2. WPF中元素拖拽的两个实例

    今天结合之前做过的一些拖拽的例子来对这个方面进行一些总结,这里主要用两个例子来说明在WPF中如何使用拖拽进行操作,元素拖拽是一个常见的操作,第一个拖拽的例子是将ListBox中的子元素拖拽到ListV ...

  3. WPF如何实现拖拽打开文件(将文件拖进窗体打开)

    在WPF中的实现和WinForm中的实现99%相似,将要实现接受拖拽释放的控件添加DragEnter事件和Drop事件,本例中控件Grid grid作为接受控件,添加事件操作如下: private v ...

  4. 【WPF】鼠标拖拽功能DragOver和Drop

    在Winform里面实现拖入功能只要设置控件AllowDrop=true; 然后实现方法 //拖入 private void txtInputPath_DragOver(object sender, ...

  5. [WPF][ListBox]鼠标拖拽多选,(Shift Key、Ctrl Key多选有效)(转)

    <ListBox Name="listBox" SelectionMode="Extended"> <ListBox.Resources> ...

  6. 【WPF/C#】拖拽Image图片控件

    需求:使得Image图片控件能够被拖动. 思路:关键是重写Image控件的几个鼠标事件,实现控制. 前台: <Image Source="C:\Users\Administrator\ ...

  7. Qt无边框窗体-最大化时支持拖拽还原

    目录 一.概述 二.效果展示 三.demo制作 1.设计窗体 2.双击放大 四.拖拽 五.相关文章 原文链接:Markdown模板 一.概述 用Qt进行开发界面时,既想要实现友好的用户交互又想界面漂亮 ...

  8. 理解事件捕获。在限制范围内拖拽div+吸附+事件捕获

    一.实现的效果是在限制范围内拖拽div+吸附+事件捕获. 这里需要理解的是事件捕获,这个事件捕获也是为了兼容div在拖拽过程中,文本不被选中这个问题. 如此良辰美景,拖拽也可以很洒脱哈.先看看图, 二 ...

  9. 解决Duilib集成CEF浏览器在Win10无法向客户区拖拽文件

    在Duilib中集成CEF浏览器项目实际开发中,遇到一个问题. 一个需求从资源管理器(桌面)拖拽文件到客户端,窗口捕获WM_DROPFILES消息然后进行消息处理,但客户区是集成的CEF浏览器,浏览器 ...

随机推荐

  1. matplotlib 绘制多个图——两种方法

    import numpy as np import matplotlib.pyplot as plt #创建自变量数bai组du x= np.linspace(0,2*np.pi,500) #创建函数 ...

  2. this.$options.data()实战之重置data

    刚刚看到这个方法学习了一下,然后想到正在开发的项目有一个需要重置data的操作,正好拿来使用一下,节省了好多代码,美滋滋...

  3. 题解-CF163E e-Government

    题面 CF163E e-Government 给 \(n\) 个字符串 \(s_i\) 和 \(q\) 个询问,刚开始字符串都服役.每次操作将集合中的一个字符串设为退役或服役,或查询与文本串 \(S_ ...

  4. 深入理解Java虚拟机(三)——垃圾回收策略

    所谓垃圾收集器的作用就是回收内存空间中不需要了的内容,需要解决的问题是回收哪些数据,什么时候回收,怎么回收. Java虚拟机的内存分为五个部分:程序计数器.虚拟机栈.本地方法栈.堆和方法区. 其中程序 ...

  5. Jmeter之登录接口参数化实战

    为了纪念我走过的坑(为什么有些简单的问题就是绊住我了,还是不够细啊) Jmeter之接口登录参数化实战 因为想要在登录时使用不同的数据进行测试,所以我选择了将数据进行参数化.因为涉及到新建一个接口的功 ...

  6. python 字典常用操作

    字典键是唯一的,但值则不是 一个简单的字典 dict = {"guo":"1106","tang":"0809",&qu ...

  7. js上 六、运算符-2

    6.1.关系运算符 用来进行比较的.比较的结果通常是布尔值真和假. ü ==:相等,只比较值是否相等 ü ===:全等,比较值的同时比较数据类型是否相等 ü !=:不相等,比较值是否不相等 ü !== ...

  8. [日常摸鱼]bzoj1036 [ZJOI2008]树的统计Count

    听说后天会考x 省选居然还考模板题的么(好吧好像NOI也有考而且也是树剖-) 题意:一棵树,每个点有权值,三种操作:单点修改.求链上最大值.求链上权值和. 直接上模板. 我可能不会写单点修改的线段树了 ...

  9. BloomFilter中保存的数据量

    结果 /** * @author WeiJiQian * BF_CARDINAL_THRESHOLD BF_FALSE_POSITIVE_RATE 保存的数据量 * 100,0000 0.01 391 ...

  10. SpringBoot事件监听机制及观察者模式/发布订阅模式

    目录 本篇要点 什么是观察者模式? 发布订阅模式是什么? Spring事件监听机制概述 SpringBoot事件监听 定义注册事件 注解方式 @EventListener定义监听器 实现Applica ...