轉帖|其它|編輯:郝浩|2011-03-09 13:18:27.000|閱讀 1201 次
概述:Silverlight的版本不斷更新.當然有些很不錯的功能和屬性添加進來并進一步得到完善. 例如拖拽. 在Silverlight 3.0版本以前是不直接支持拖拽效果. 同樣在Ria運用中我也對比一個Flex實現拖拽方式, 其實就是利用一個DragManager類,這是一個像StartDrag靜態方法的類,你只需要提供一個 UIComponent對象,DragManager就會創建一個微小的透明圖像跟隨鼠標,跟隨鼠標的圖像經過組件上時會很形像的表明是否允許接受拖拽對象. 實現拖拽效果.
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
Silverlight的版本不斷更新.當然有些很不錯的功能和屬性添加進來并進一步得到完善. 例如拖拽. 在Silverlight 3.0版本以前是不直接支持拖拽效果. 同樣在Ria運用中我也對比一個Flex實現拖拽方式, 其實就是利用一個DragManager類,這是一個像StartDrag靜態方法的類,你只需要提供一個 UIComponent對象,DragManager就會創建一個微小的透明圖像跟隨鼠標,跟隨鼠標的圖像經過組件上時會很形像的表明是否允許接受拖拽對象. 實現拖拽效果.
在Silverlight 3.0中做過拖拽效果應該知道, 當你分析了Drag拖拽效果步驟. 在silverlight 3.0以前中實現的 難點核心是如何保持生成的代理形象與鼠標進行同步. 而在4.0中大大增強了控件之間的拖拽行為.現在在Silverlight 4中, 針對所有的UIElement對象,增加了一個AllowDrop屬性 設置為True,我們甚至可以把實體檔案拖曳到瀏覽器上正在執行的Silverlight應用程序中.相比3.0 足以激動人心. 轉入正題Silverlight 4下拖拽效果實現.
實現效果: 從一個listbox某一個子項移動到另外一個listbox中.
A:在拖放控件之前添加一個Toolkit空間引用:
1 xmlns:toolKit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit">
B:拖入第一個ListBox.注意是包含在toolKit:ListBoxDragDropTarget控件中,并設置 AllowDrop="True"
1 <toolKit:ListBoxDragDropTarget AllowDrop="True">
2 <ListBox x:Name="customerListBoxMain" Height="200" Width="200"
3 DisplayMemberPath="Name">
4 <ListBox.ItemsPanel>
5 <ItemsPanelTemplate>
6 <StackPanel Orientation="Vertical"/>
7 </ItemsPanelTemplate>
8 </ListBox.ItemsPanel>
9 </ListBox>
10 </toolKit:ListBoxDragDropTarget>
其實在去年Silverlight 4.0還是Beta版本時. 這個AllowDrop并沒有被直接作為ListBoxDragDropTarget屬性來定義.silverlight 4.0 Beta版本實現AllowDrop是通過引用空間:
1 xmlns:mswindows="clr-namespace:Microsoft.Windows;assembly=System.Windows.Controls.Toolkit"
在定義toolKit:ListBoxDragDropTarget控件設置屬性為mswindows下的DragDrop.AllowDrop="True".
在定義toolKit:ListBoxDragDropTarget控件設置屬性為mswindows下的DragDrop.AllowDrop="True".當然4.0正式版后不用這么做 則直接通過在每個UIElement中設置Bool類型AllowDrop屬性,更加簡便
toolkit:ListBoxDragDropTarget mswindows:DragDrop.AllowDrop="True"> <!--包含Listbox控件--> </toolkit:ListBoxDragDropTarget>
C:拖入第二個接受ListBox基本可第一個雷同:
1 <toolKit:ListBoxDragDropTarget AllowDrop="True">
2 <ListBox x:Name="AcceptListBox" Height="200" Width="200" DisplayMemberPath="Name">
3 <ListBox.ItemsPanel>
4 <ItemsPanelTemplate>
5 <StackPanel Orientation="Vertical"/>
6 </ItemsPanelTemplate>
7 </ListBox.ItemsPanel>
8 </ListBox>
9 </toolKit:ListBoxDragDropTarget>
D:綁定數據源.為了達到演示Drag效果的演示目的,定義一個Person類.類中只有一個屬性Name.
1 public class Person
2 {
3 public string Name { get; set; }
4 }
通過在PersonDataProvider類中定義一個方法提供一個ObservableCollection<Person>集合數據.綁定到第一個[customerListBoxMain]ListBox中.
1 public class PersonDataProvider
2 {
3 public static ObservableCollection<Person> GetData()
4 {
5 return new ObservableCollection<Person>
6 {
7 new Person { Name = "Akash Sharma" },
8 new Person { Name = "Vinay Sen" },
9 new Person { Name = "Lalit Narayan" },
10 new Person { Name = "Madhumita Chatterjee" },
11 new Person { Name = "Priyanka Patil" },
12 new Person { Name = "Kumar Sanu" },
13 new Person { Name = "Victor Kapoor" },
14 new Person { Name = "Shymal Sen" },
15 new Person { Name = "Alan D'Souza" },
16 new Person { Name = "Kamal Saha" },
17 new Person { Name = "Alex Chan" },
18 new Person { Name = "Rohit Sharma" },
19 new Person { Name = "Dipti Sen" },
20 new Person { Name = "Dinesh Sharma" },
21 new Person { Name = "Kamal Kapoor" },
22 new Person { Name = "Raj Kapoor" },
23 new Person { Name = "Deepa Karmakar" },
24 new Person { Name = "Sarmishtha Chakrobarty" },
25 new Person { Name = "Pranab Kumar Debnath" },
26 new Person { Name = "Hiral Grover" },
27 new Person { Name = "Munmun Patel" },
28 new Person { Name = "Santosh Kumar Sen" },
29 new Person { Name = "Sandeep Debnath" }
30 };
31 }
32 }
MainPage后臺中進行數據綁定:
1 public MainPage()
2 {
3 InitializeComponent();
4 customerListBoxMain.ItemsSource = PersonDataProvider.GetData();
6 }
綁定完成后.編譯通過開始運行 先看看效果.
ok.silverlight 4.0中實現拖拽如此簡單.當使用了ListBoxDragDropTarget控件后
A:兩個listBox之間可以相互拖放ListBoxItem項 實現相互的拖拽效果
B:可以在一個Listbox中通過drag/drop行為對ListBox中的項目進行重新排列. [強悍吧]
注意:在操作B時.不過ListBoxDragDropTarget并不能作用于virtualized容器(ListBox的默認容器),所以必須改變ListBox的ItemsPanelTemplate容器,比如說像這樣加個StackPanel進去取代virtualized默認容器即可 .
當然更厲害的ListBoxDragDropTarget它支持任意的DataTemplate, 拖拽效果仍然成立.[這里不再演示]!!!
當然你也許會問.我想實現Treeview中類似的拖拽效果是否能夠實現?. 答案是肯定 因為除了ListBoxDragDropTarget控件還有如下控件也支持這種效果:
A: ListBoxDragDropTarget[已實現]
B: TreeViewDragDropTarget -[實現Treeview拖拽]
C: DataGridDragDropTarget-[DataGrid控件支持]
D: DataPointSeriesDragDropTarget-[尚未測試.]
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:網絡轉載