我从ReSharper收到一条关于从我的对象构造函数调用虚拟成员的警告.
为什么不做这件事?
我使用C#winforms有一段时间的项目.我在Windows 7发布之前实现了拖放功能.工作就像一个魅力.但是,使用Windows 7时它不起作用.该事件甚至没有被触发.
AllowDrop设置为true.订阅DragEnter它时,不会在Windows 7中调用(不确定vista).但是在XP上它一直都有效.该程序使用administritave priviliges运行.
Windows 7与xp的拖拽有什么不同吗?不知道它是否相关,但我使用的是x64
如何通过拖放将文件加载到表单?
会出现哪个事件?
我应该使用哪个组件?
以及如何在将文件拖放到表单后确定文件名和其他属性?
谢谢!
码
private void panel1_DragEnter(object sender, DragEventsArgs e){
if (e.Data.GetDataPresent(DataFormats.Text)){
e.Effect = DragDropEffects.Move;
MessageBox.Show(e.Data.GetData(DataFormats.Text).toString());
}
if (e.Data.GetDataPresent(DataFormats.FileDrop)){
}
}
Run Code Online (Sandbox Code Playgroud)
好的,这很有效.
文件怎么样?我怎样才能获得文件名和扩展名?
这只是一个dragEnter动作.
我正在尝试创建一个Windows窗体,我可以删除文件/文件夹.
我在WinForms应用程序中有以下代码
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
Debug.Print("DragEnter");
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
MessageBox.Show("Dropped!");
}
}
Run Code Online (Sandbox Code Playgroud)
我已将AllowDrop属性设置为true.我已经尝试在Visual Studio中的调试中运行该应用程序.基于其他类似问题的答案,我尝试以管理员身份运行已编译的exe.我已经尝试不以管理员身份运行已编译的exe .
但无论我做什么,我都无法触发DragDrop事件.但是,DragEnter事件会触发.我错过了什么?
我想知道如何拖放文件夹并获取其名称.我已经知道如何使用文件,但我不确定如何修改它以便能够拖动文件夹.这是删除文件时触发的事件代码:
private void checkedListBox_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
// NB: I'm only interested in the first file, even if there were
// multiple files dropped
string fileName = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
}
}
Run Code Online (Sandbox Code Playgroud) 我想使用MVVM模式在多个数据绑定的列表框上实现拖放.我不是想在列表框之间拖放,而是希望用户能够在每个列表框中拖放listboxitems,以便他们可以重新排列排序顺序.我在SO上发现这篇帖子非常有帮助:
我想尝试使这些方法更"通用",以便它可以在任何绑定到不同类型的Observable Collections的列表框上工作.所以说这是我在VIEW中的XAML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="{x:Type ListBoxItem}" x:Key="ListBoxItemDragDrop">
<Setter Property="AllowDrop" Value="True" />
<EventSetter Event="PreviewMouseMove" Handler="ListBoxItem_PreviewMouseMoveEvent" />
<EventSetter Event="Drop" Handler="listbox1_Drop" />
</Style>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ListBox Name="listbox1"
ItemsSource="{Binding OCofType1}"
ItemContainerStyle="{StaticResource ListBoxItemDragDrop}" />
<ListBox Name="listbox2" Grid.Column="1"
ItemsSource="{Binding OCofType2}"
ItemContainerStyle="{StaticResource ListBoxItemDragDrop}"/>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
OC绑定的位置是ObservalbeCollection <Type1>和ObservalbeCollection <Type2>.这是VIEW中抓取鼠标移动事件并检查它是否是拖动的方法:
void ListBoxItem_PreviewMouseMoveEvent(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed && sender is ListBoxItem)
{
ListBoxItem draggedItem = (ListBoxItem)sender;
DragDrop.DoDragDrop(draggedItem, draggedItem.DataContext, DragDropEffects.Move);
draggedItem.IsSelected …Run Code Online (Sandbox Code Playgroud)