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. linux 复制文件前n行到另外一个文件

    head -n 100 train.txt > 123.txt head -100 train.txt > 123.txt

  2. nginx学习之——虚拟主机配置

    例子1: 基于域名的虚拟主机 server { listen 80;  #监听端口 server_name a.com; #监听域名 location / { root /var/www/a.com; ...

  3. redis学习之——在分布式数据库中CAP原理CAP+BASE

    分布式系统 分布式系统(distributed system) 由多台计算机和通信的软件组件通过计算机网络连接(本地网络或广域网)组成.分布式系统是建立在网络之上的软件系统.正是因为软件的特性,所以分 ...

  4. springboot中过滤器、拦截器、切片使用

    直接贴代码:采用maven工程 pom.xml <?xml version="1.0" encoding="UTF-8"?> <project ...

  5. EM 算法-对鸢尾花数据进行聚类

    公号:码农充电站pro 主页:https://codeshellme.github.io 之前介绍过K 均值算法,它是一种聚类算法.今天介绍EM 算法,它也是聚类算法,但比K 均值算法更加灵活强大. ...

  6. jenkins配置--上传代码,定时执行用例,发送测试报告

    1.安装条件:jdk1.8以上的,百度jenkin进入官网--download ,根据需要的版本下载 2.jenkins概念:持续集成,jenkins开源属于插件式形式进行管理的,选择性的装插件,支持 ...

  7. 访问控制列表ACL应用

    ACL的应用的场景 应用在三层接口 • Nat地址转换 Nat(network address translation,地址转换)是将数据报报头中的ip地址转换为另一个ip地址的过程,主要用于实现内部 ...

  8. Bug java 安全证书

    在一个改修项目里,安全性方面总是出一个bug,错误信息如下: xception in thread "main" javax.crypto.BadPaddingException: ...

  9. oracle 常用语句2

    -- number(38) -- char(2000) -- varchar(4000) create table student( sno number(3) primary key, sname ...

  10. CSS练习 —— css选择器

    CSS选择器就是 通过选择器来 定位 你要控制的样式的部分,分为以下几种 1.HTML选择符(标签选择器) 就是把HTML标签作为选择符使用 如 p {.......} 网页中所有的P标签采用此样式 ...