标签:
简单拖拽的实现是,实现源控件的MouseDown事件,和目标控件Drop事件。调用DragDrop.DoDragDrop()以启动拖放操作,DragDrop.DoDragDrop()函数接受三个参数:dragSource、data以及allowedEffects。特别需要注意的 是dragSource参数。该参数标示了拖拽操作的消息源,也决定了所有的消息源事件由谁发出。参数data则用来包装Drag&Drop所操 作的数据。一般情况下,其都是一个DataObject类型的实例。该实例内部应包装拖拽所实际操作的数据。最后,allowedEffects可以用来 指定拖拽操作的效果。调用该函数的片断可以如下所示:
DragDrop.DoDragDrop(mListBox, dataObject, DragDropEffects.Copy);
例子1:从groupBox中拖动TextBlock控件到右侧Listbox中
xmal:
<Grid> <ListBox x:Name="listBox" Drop="listBox_Drop" HorizontalAlignment="Left" Height="190" Margin="285,10,0,0" VerticalAlignment="Top" Width="159" AllowDrop="True"/> <GroupBox x:Name="groupBox" Header="GroupBox" MouseDown="TextBlock_MouseDown" HorizontalAlignment="Left" Margin="23,10,0,0" VerticalAlignment="Top" Height="190" Width="247"> <StackPanel Margin="10"> <TextBlock Margin="10" >足球</TextBlock> <TextBlock Margin="10">篮球</TextBlock> <TextBlock Margin="10">羽毛球</TextBlock> <TextBlock Margin="10">乒乓球</TextBlock> </StackPanel> </GroupBox> </Grid>
C#
private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e) { TextBlock obj = (TextBlock)e.OriginalSource; DragDrop.DoDragDrop(obj, obj.Text, DragDropEffects.Copy); } private void listBox_Drop(object sender, DragEventArgs e) { string data = e.Data.GetData(DataFormats.Text).ToString(); listBox.Items.Add(data); }
代码不难,自己应该可以看懂,这里不解释了.
例子2:从外部拖拽一个文件夹到窗体彩色框处,将显示文件夹地址和文件夹下所有文件信息
代码如下:
<Grid Drop="textBox_Drop"> <TextBlock x:Name="textBlock" Text="文件夹:" HorizontalAlignment="Left" Margin="10,35,0,0" TextWrapping="Wrap" VerticalAlignment="Top"/> <ListBox x:Name="listBox" HorizontalAlignment="Left" Height="176" Margin="10,100,0,0" VerticalAlignment="Top" Width="391"/> <TextBlock x:Name="textBlock1" HorizontalAlignment="Left" Margin="10,72,0,0" TextWrapping="Wrap" Text="文件夹下文件:" VerticalAlignment="Top"/> <TextBlock x:Name="textBlock2" HorizontalAlignment="Left" Margin="10,10,0,0" TextWrapping="Wrap" Text="请拖动文件夹到彩色框中" VerticalAlignment="Top"/> <TextBlock x:Name="txtfloder" HorizontalAlignment="Left" Margin="63,31,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="328" Background="#FFCEDA41" Height="34"/> </Grid>
private void textBox_Drop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { listBox.Items.Clear(); string floder = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString(); if (Directory.Exists(floder)) { txtfloder.Text = floder.ToString(); foreach (var item in Directory.GetFiles(floder)) { listBox.Items.Add(item); } } else txtfloder.Text = "无效文件夹"; } }
例子3:把园从左拖动到右侧
<Grid>
<Canvas Name="canvas1" HorizontalAlignment="Left" Height="259" Margin="10,10,0,0" VerticalAlignment="Top" Width="228" Background="#FFEBEABC">
<Ellipse Name="ell" PreviewMouseDown="ell_PreviewMouseMove" PreviewMouseMove="ell_PreviewMouseMove" Fill="#FF9595E5" Height="100" Canvas.Left="28" Stroke="Black" Canvas.Top="23" Width="100"/>
</Canvas>
<Canvas Name="canvas2" Drop="c2_Drop" DragOver="canvas2_DragOver" HorizontalAlignment="Left" Height="259" Margin="256,10,0,0" VerticalAlignment="Top" Width="214" Background="#FFEDE2E2" AllowDrop="True"/>
</Grid>
private void ell_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
DataObject obj = new DataObject(typeof(Ellipse), ell);
DragDrop.DoDragDrop(this.ell, obj, DragDropEffects.Move);
}
}
private void c2_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(Ellipse)))
{
Ellipse obj = e.Data.GetData(typeof(Ellipse)) as Ellipse;
canvas1.Children.Remove(obj);
canvas2.Children.Add(obj);
}
}
private void canvas2_DragOver(object sender, DragEventArgs e)
{
if (!e.Data.GetDataPresent(typeof(Ellipse)))
{
e.Effects = DragDropEffects.None;
e.Handled = true;
}
}
这里的事件 PreviewMouseDown="ell_PreviewMouseMove" PreviewMouseMove="ell_PreviewMouseMove" ,可以只写其中一个即可。
例子4:
标签:
原文地址:http://www.cnblogs.com/lunawzh/p/4808133.html