大佬教程收集整理的这篇文章主要介绍了Silverlight4不同控件间拖拽实现-附源码下载,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
前天整理一份Silverlight 4中拖拽效果实现-附源码下载 在评论中一位园友提到实现不同控件间拖拽效果设想. 其实在项目中原本我遇到最初拖拽实现就是一个从TreeView中拖拽到一个ListBox中.中午正好看了Silverlight.net BBS和CrraySun.com上讨论.做了一个简单Demo实现.先看看效果:
左边ListBox,右边Treeview.实现步骤如下
A:页面布局:
- <Grid x:Name="LayoutRoot" @R_197_11442@kground="White" HorizontalAlignment="Center" VerticalAlignment="Center">
- <Grid.RowDeFinitions>
- <RowDeFinition Height="40"></RowDeFinition>
- <RowDeFinition Height="350"></RowDeFinition>
- <RowDeFinition Height="10*" />
- </Grid.RowDeFinitions>
- <Grid.columnDeFinitions>
- <columnDeFinition Width="300"></columnDeFinition>
- <columnDeFinition Width="300"></columnDeFinition>
- <columnDeFinition Width="200*" />
- </Grid.columnDeFinitions>
- <TextBlock text="Drag ListBox to Treeview" Foreground="Red" FontFamily="Comic Sans MS" FontSize="16" Grid.columnSpan="2" Margin="85,12,162,0"></TextBlock>
- <!--左边一个listBox-->
- <toolKit:ListBoxDragDropTarget AllowDrop="True" Grid.Row="1" Grid.column="0" >
- <ListBox x:Name="customerList@L_401_5@main" Height="240" Width="215" SELEctionMode="Extended" DisplaymemberPath="CustomerName" BorderBrush="Black" BorderThickness="1">
- <ListBox.ItemsPanel>
- <ItemsPanelTemplate>
- <StackPanel Orientation="Vertical"/>
- </ItemsPanelTemplate>
- </ListBox.ItemsPanel>
- </ListBox>
- </toolKit:ListBoxDragDropTarget>
- <!--右边一个Treeview-->
- <toolKit:TreeViewDragDropTarget AllowDrop="True" Grid.Row="1" Grid.column="1" BorderThickness="1" BorderBrush="Red">
- <!--定义一个数据模板-->
- <toolKit:TreeViewDragDropTarget.resources>
- <my:HierarchicalDataTemplate x:Name="datetmp" Itemssource="{Binding Customer}">
- <TextBlock text="{Binding CustomerNamE}"/>
- </my:HierarchicalDataTemplate>
- </toolKit:TreeViewDragDropTarget.resources>
- <sdk:TreeView x:Name="AcceptTreeview" Height="240" Width="215" ItemTemplate="{Staticresource datetmp}"> </sdk:TreeView>
- </toolKit:TreeViewDragDropTarget>
- </Grid>
同上篇不同在TreeView中使用到了HierarchicalDataTemplate数据模板来定义Treeview数据显示. HierarchicalDataTemplate 数据模板默认是不添加的,所以需要在页面添加引用如下:
至于HierarchicalDataTemplate如何使用请参见MSDN.
同样为了演示方便 直接写了一个类用来ListBox和Treeview中提供所需数据.
- /// <sumMary>
- /// 为了达到演示目的 当前类提供list数据
- /// Author:chenKai Date:2010年5月28日10:21:36
- /// </sumMary>
- public class ProvideDate
- {
- public static List<Customer> GetAllCustomerList()
- {
- List<Customer> getcuslist = new List<Customer>();
- getcuslist.Add(new Customer { CustomerName="JackChen" });
- getcuslist.Add(new Customer { CustomerName = "Arrmy" });
- getcuslist.Add(new Customer { CustomerName = "SunSkyUnion" });
- getcuslist.Add(new Customer { CustomerName = "西藏拉萨" });
- getcuslist.Add(new Customer { CustomerName = "甘肃玉门关" });
- return getcuslist;
- }
- public static List<Customer> GetAllCustomerTreeList()
- {
- List<Customer> getcuslist = new List<Customer>();
- getcuslist.Add(new Customer { CustomerName = "markChen" });
- getcuslist.Add(new Customer { CustomerName = "KaiDun" });
- getcuslist.Add(new Customer { CustomerName = "GuideInformation" });
- getcuslist.Add(new Customer { CustomerName = "三门峡函谷关" });
- getcuslist.Add(new Customer { CustomerName = "嘉峪关" });
- getcuslist.Add(new Customer { CustomerName = "泰山" });
- getcuslist.Add(new Customer { CustomerName = "嵩山" });
- return getcuslist;
- }
- }
- public class Customer
- {
- public String CustomerName { get; set; }
- }
页面数据绑定:
- //绑定ListBox数据
- List<Customer> getcuslist = TestSilverlightOutOfBrowerDemo.Date.ProvideDate.GetAllCustomerList();
- if (getcuslist != null)
- {
- ObservableCollection<Customer> getlist = new ObservableCollection<Customer>();
- foreach (Customer getcus in getcuslist)
- {
- getlist.Add(getcus);
- }
- this.customerList@L_401_5@main.Itemssource = getlist;
- }
- //绑定Treeview数据
- List<Customer> gettreeviewlist=TestSilverlightOutOfBrowerDemo.Date.ProvideDate.GetAllCustomerTreeList();
- ObservableCollection<Customer> getviewlist = new ObservableCollection<Customer>();
- foreach (Customer getcustree in gettreeviewlist)
- {
- getviewlist.Add(getcustreE);
- }
- this.AcceptTreeview.Itemssource = getviewlist;
因为使用ObservableCollection一个动态数据集合,需要在添加一个空间引用:
ObservableCollection动态数据集合,这次数据通过绑定来实现.动态数据集合,在添加项、移除项或刷新整个列表时,此集合将提供通知 具体参见:@L_607_59@
如上即简单实现了一个listBox向一个TreeView中拖拽实现,其实我当初在实现这问题,参考大量资料. 我想说一下个人对这个拖拽实现思路理解.首先我想说的是在整个拖拽过程事件执行过程,
注意:虽然实现了Treeview在ListBox之间拖拽,但真正拖拽事件发生是在TreeViewDragDropTarget和ListBoxDragDropTarget控件中. 不要误认为是ListBox或Treeview本身.
(1)先看看TreeViewDragDropTarget关于Drag事件.
分别为Drop,DragOver,DragLeave,DragEnter. 这三个真正执行顺序如下:
执行场景:当把一个ListBox一个项拖拽到Treeview时 从DragEnter开始执行到DragOver. 其实上面实现完全是利用数据模板和DragDropTraget控件便利. 来@R_262_10186@在Sl3.0中实现一个拖拽需要具体步骤拆分:
A:实现一个拖动图像,作为开始拖拽时的快照
B:找到程序的root visual根视觉(如StackPanel)
C:将拖动图像添加到根视觉,并得到它的绝对坐标[动态坐标数据-难点]
D:在运动时保持鼠标与拖动图像同步,随时给出是否在落下目标(drop target)上的视觉反馈 [需要动画效果]
E:处理用户释放鼠标时刻,当经过落下目标时可以适当地动作,甚至可以显示一段不错的动画
如此就在Silverlight 3.0拖拽效果.当然在4.0加以封装来实现.如何来获得拖拽实时数据 和SL3.0有点不同.
(2)获得拖拽实时数据
其实拖拽时listBox中数据都放在ItemContainer数据容器中,当拖拽一项时既是反映到ItemContainer中就是Remove删除一项,反而言之 接受一方Treeview中数据容器这是Add添加一个新项. 这就给我们提供一个监听拖拽数据机会.我们可以在listBox中ItemContainerGenerator.ItemsChanged定义一个监听事件. 来获取当前拖拽项.
事件实现:
- void ItemContainerGenerator_ItemsChanged(object sender, System.Windows.Controls.Primitives.ItemsChangedEventArgs E)
- {
- if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
- {
- //e.Position获取集合中更改发生的位置
- int index = (e.Position.Index + e.Position.offset);
- messageBox.Show("@R_206_10586@l:" + this.AcceptTreeview.Items.Count.ToString() + "Index:" + index.ToString());
- object obj = this.AcceptTreeview.Items[index];
- if (obj != null)
- {
- TreeViewItem getitem = obj as TreeViewItem;
- messageBox.Show(getitem.Header.ToString());
- ListBoxItem getBoxitem = obj as ListBoxItem;
- messageBox.Show(getBoxitem.Content.ToString());
- }
- }
- }
通过附属的ItemsChangedEventArgs 附带事件信息. 来判断当前对数据容器ItemContainer操作类型,System.Collections.Specialized.NotifyCollectionChangedAction是一个操作枚举. 包含None Add REmove. 判断拖拽状态后即可通过E.Position来获取数据项发生的位置. 集合通过装换获得具体拖拽项.当然也可定义其他操作.
以上是大佬教程为你收集整理的Silverlight4不同控件间拖拽实现-附源码下载全部内容,希望文章能够帮你解决Silverlight4不同控件间拖拽实现-附源码下载所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。