Win 8中WPF listview与listBox的Drag、Drop操作

本文涉及的产品
应用型负载均衡 ALB,每月750个小时 15LCU
网络型负载均衡 NLB,每月750个小时 15LCU
传统型负载均衡 CLB,每月750个小时 15LCU
简介:

Win 8中WPF  listview与listBox的Drag、Drop操作。

基本原理是将listview中的项拖动到listBox中。

界面:

<UserControl x:Class= "DragTitleToWebView.MainPage"
     xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x= "http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:d= "http://schemas.microsoft.com/expression/blend/2008"
     xmlns:mc= "http://schemas.openxmlformats.org/markup-compatibility/2006"
     mc:Ignorable= "d"
     d:DesignHeight= "768"  d:DesignWidth= "1366" >
     
     <Grid x:Name= "LayoutRoot"  Background= "#FF0C0C0C" >
         <Grid.ColumnDefinitions>
             <ColumnDefinition Width= "1*" />
             <ColumnDefinition Width= "5*" />
         </Grid.ColumnDefinitions>
         <ListView Grid.Column= "0"  AllowDrop= "True"  CanDragItems= "True"   CanReorderItems= "True"  DragItemsStarting= "ListView_DragItemsStarting"  >
             <ListView.Resources>
                 <Style TargetType= "Rectangle" >
                     <Setter Property= "Width"  Value= "100" />
                     <Setter Property= "Height"  Value= "100" />
                 </Style>
                 </ListView.Resources>
             <Rectangle Fill= "Orange"   Tag= "Rect1"  Width= "100"  Height= "100" />
             <Rectangle Fill= "Orange"  Tag= "Rect2"  Width= "120"  Height= "120" />
             <Rectangle Fill= "Orange"  Tag= "Rect3"  Width= "140"  Height= "140" />
             <Rectangle Fill= "Orange"  Tag= "Rect4"  Width= "160"  Height= "160" />
         </ListView>
         <ListBox x:Name= "lb"  Grid.Column= "1"  Margin= "10" >
            
         </ListBox>
         <Rectangle  Margin= "10"  x:Name= "droppanel"  Opacity= "0.01"  Visibility= "Collapsed"  Grid.Column= "1"  AllowDrop= "True"  Drop= "droppanel_Drop"  Fill= "Green" />
     </Grid>
     
</UserControl>

 Code:

 

using  Windows.UI.Xaml.Media;
using  Windows.UI.Xaml.Shapes;
partial  class  MainPage
{
     private  bool  flag = false ;
     public  MainPage()
     {
         InitializeComponent();
     }
 
     private  void  ListView_DragItemsStarting( object  sender, DragItemsStartingEventArgs e)
     {
         e.Data.SetText( "t" , (e.Items[0] as  Rectangle).Tag.ToString());
         e.Data.SetText( "width" , (e.Items[0] as  Rectangle).Width.ToString());
         e.Data.SetText( "height" , (e.Items[0] as  Rectangle).Height.ToString());
 
         droppanel.Visibility = Windows.UI.Xaml.Visibility.Visible;
         lb.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
         if  (flag)
         {
             lb.Visibility = Windows.UI.Xaml.Visibility.Visible;
         }
     }
 
     private  void  droppanel_Drop( object  sender, DragEventArgs e)
     {
         string  tag = e.Data.GetText( "t" );
         //如果已经有相同的item,则返回
         foreach  (Rectangle item in  lb.Items)
         {
             if  (item.Tag.ToString() == tag)
             {
                 return ;
             }
         }
         int  width = int .Parse(e.Data.GetText( "width" ));
         int  height = int .Parse(e.Data.GetText( "height" ));
         Rectangle rect = new  Rectangle();
         rect.Tag = tag;
         rect.Height = height;
         rect.Width = width;
         SolidColorBrush b = new  SolidColorBrush(Colors.Orange);
         rect.Fill = b;
 
         lb.Items.Add(rect);
 
         droppanel.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
         lb.Visibility = Windows.UI.Xaml.Visibility.Visible;
 
         flag = true ;
     }
}

如图:Win 8中是全屏,这里只是图片的一部。

参考


本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/archive/2012/02/29/2373855.html,如需转载请自行联系原作者

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
目录
相关文章
|
存储 搜索推荐 C#
WPF/C#:让绘制的图形可以被选中并将信息显示在ListBox中
WPF/C#:让绘制的图形可以被选中并将信息显示在ListBox中
139 0
|
数据处理 C# 索引
WPF技术之ListView控件
WPF ListView控件是一个用来显示集合数据的控件,它以表格形式呈现数据,并支持对数据进行排序、筛选和操作。
562 0
|
C# 虚拟化 开发者
WPF技术之ListBox控件
WPF ListBox控件是一种用于显示和选择多个项的常用控件。它可以展示任意类型的数据,并允许用户通过鼠标或键盘进行选择操作
1452 0
|
C#
C# WPF 中用代码模拟鼠标和键盘的操作
原文:C# WPF 中用代码模拟鼠标和键盘的操作   原文地址   C#开发者都知道,在Winform开发中,SendKeys类提供的方法是很实用的。
2369 0
|
前端开发 C#
【C#/WPF】ListView的MVVM例子,及禁止拖动ListView的头部Header
原文:【C#/WPF】ListView的MVVM例子,及禁止拖动ListView的头部Header 一个ListView的MVVM简单例子: ...
1685 0
WPF-样式问题-处理ListBox、ListView子项内容全填充问题
WPF-样式问题-处理ListBox、ListView子项内容全填充问题
418 0
WPF-样式问题-ListBox或ListView中子项全填充去除边线问题
WPF-样式问题-ListBox或ListView中子项全填充去除边线问题
331 0
如何解决WPF中 ScrollViewer 内包含 TreeView 或者 ListBox 等控件时滚轮事件被劫持的问题
如何解决WPF中 ScrollViewer 内包含 TreeView 或者 ListBox 等控件时滚轮事件被劫持的问题
|
C#
WPF中Expander与ListBox(ItemsControl)嵌套中的问题
原文:WPF中Expander与ListBox(ItemsControl)嵌套中的问题 1. 当ListBox放在Expander中时,为了要实现实时更新数据的效果,这里使用了    ObservableCollection类型来作为数据源,         初始的简单例子如下:只有一个List...
1905 2
|
C# 索引 容器
WPF ListView控件设置奇偶行背景色交替变换以及ListViewItem鼠标悬停动画
原文:WPF ListView控件设置奇偶行背景色交替变换以及ListViewItem鼠标悬停动画 利用WPF的ListView控件实现类似于Winform中DataGrid行背景色交替变换的效果,同时增加鼠标的悬停效果。
2020 1