Silverlight 拖动客户端文件 到ListBox
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
listBox.AllowDrop = true;
listBox.DragEnter += new DragEventHandler(listBox_DragEnter);
listBox.DragLeave += new DragEventHandler(listBox_DragLeave);
listBox.DragOver += new DragEventHandler(listBox_DragOver);
listBox.Drop += new DragEventHandler(listBox_Drop);
}
void listBox_DragEnter(object sender, DragEventArgs e)
{
listBox.Items.Add("DragEnter");
}
void listBox_DragLeave(object sender, DragEventArgs e)
{
listBox.Items.Add("DragLeave");
}
void listBox_DragOver(object sender, DragEventArgs e)
{
// listBox.Items.Add("DragOver");
}
void listBox_Drop(object sender, DragEventArgs e)
{
listBox.Items.Add("Drop");
if (e.Data == null)
return;
IDataObject dataObject = e.Data as IDataObject;
FileInfo[] files = dataObject.GetData(DataFormats.FileDrop) as FileInfo[];
foreach (FileInfo file in files)
{
if (file.Exists)
listBox.Items.Add("FileName: " + file.Name + " [FileSize: " + file.Length + "]");
else
listBox.Items.Add("文件无效");
}
}
{
listBox.AllowDrop = true;
listBox.DragEnter += new DragEventHandler(listBox_DragEnter);
listBox.DragLeave += new DragEventHandler(listBox_DragLeave);
listBox.DragOver += new DragEventHandler(listBox_DragOver);
listBox.Drop += new DragEventHandler(listBox_Drop);
}
void listBox_DragEnter(object sender, DragEventArgs e)
{
listBox.Items.Add("DragEnter");
}
void listBox_DragLeave(object sender, DragEventArgs e)
{
listBox.Items.Add("DragLeave");
}
void listBox_DragOver(object sender, DragEventArgs e)
{
// listBox.Items.Add("DragOver");
}
void listBox_Drop(object sender, DragEventArgs e)
{
listBox.Items.Add("Drop");
if (e.Data == null)
return;
IDataObject dataObject = e.Data as IDataObject;
FileInfo[] files = dataObject.GetData(DataFormats.FileDrop) as FileInfo[];
foreach (FileInfo file in files)
{
if (file.Exists)
listBox.Items.Add("FileName: " + file.Name + " [FileSize: " + file.Length + "]");
else
listBox.Items.Add("文件无效");
}
}