WPF 的拖拽操作(DragDrop)
生活随笔
收集整理的這篇文章主要介紹了
WPF 的拖拽操作(DragDrop)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ? 在WPF中似乎沒有對拖拽操作進行改變,和以前的方式一樣。如果曾近在 Windows 窗體應用程序中使用過鼠標拖放,就會發現在 WPF 中的編程接口實際上沒有發生變化。重要的區別是用于拖放操作的方法和事件被集中到了 System.Windows.DragDrop 類中,然后供其他類(如 UIElement )使用。
?
? ? 本質上,拖放操作通過以下三個步驟進行:
?
下面是一個示例:
?
C#代碼:
private void lblTarget_Drop_1(object sender, DragEventArgs e){lblTarget.Content = e.Data.GetData(DataFormats.Text);}private void lblTarget_DragEnter_1(object sender, DragEventArgs e){if (e.Data.GetDataPresent(DataFormats.Text))e.Effects = DragDropEffects.Copy;elsee.Effects = DragDropEffects.None;}private void Label_MouseDown_1(object sender, MouseButtonEventArgs e){DragDrop.DoDragDrop(lblControl, lblControl.Content, DragDropEffects.Copy);}XAML代碼:
<Window x:Class="_1019_DragAndDrap.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"><Grid><Grid.RowDefinitions><RowDefinition/><RowDefinition/></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions><TextBox Name="txtStr" Padding="10" VerticalAlignment="Center" HorizontalAlignment="Center">Drag from this text</TextBox><Label Name="lblControl" Padding="10" Background="LightGoldenrodYellow" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Center" MouseDown="Label_MouseDown_1">Or this label</Label><Label Name="lblTarget" Padding="10" Background="LightGoldenrodYellow" Grid.ColumnSpan="2" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center" AllowDrop="True" DragEnter="lblTarget_DragEnter_1" Drop="lblTarget_Drop_1">To this label</Label></Grid> </Window>?
源碼下載:http://files.cnblogs.com/andrew-blog/1019_DragAndDrap.rar
使用工具:VS2012
參考:http://www.wxzzz.com/?id=131
轉載于:https://www.cnblogs.com/andrew-blog/p/WPF_DragDrop.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的WPF 的拖拽操作(DragDrop)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SCCM 2012系列之一 Opera
- 下一篇: Win8 .NET Framework