WPF拖动DataGrid中的数据到ListBox
1、效果图:

2、XAML
<Window x:Class="WpfApplication2.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" Loaded="Window_Loaded">
<Grid>
<ListBox Height="287" HorizontalAlignment="Left" Margin="12,12,0,0" Name="m_ListBox" VerticalAlignment="Top" Width="148" SelectionMode="Single" AllowDrop="True" Drop="m_ListBox_Drop" DragOver="m_ListBox_DragOver">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Id, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
<TextBlock Text="{Binding Path=Name, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
<TextBlock Text="{Binding Path=ChildCount, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
<Button Content="展示" Tag="{Binding}" Name="m_ListBoxButton" Click="m_ListBoxButton_Click" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<DataGrid AutoGenerateColumns="False" Height="287" HorizontalAlignment="Left" Margin="181,12,0,0" Name="m_DataGrid" VerticalAlignment="Top" Width="299" AllowDrop="True" SelectionMode="Extended" PreviewMouseLeftButtonDown="m_DataGrid_PreviewMouseLeftButtonDown">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Id}" IsReadOnly="True" />
<DataGridTextColumn Binding="{Binding Name}" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
3、CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel; namespace WpfApplication2
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private ObservableCollection<Person> listBoxList = new ObservableCollection<Person>();
private Person m_SelectedListBoxPerson;
private Button m_SelectedListBoxButton; public MainWindow()
{
InitializeComponent();
} private void Window_Loaded(object sender, RoutedEventArgs e)
{
Person person = new Person() { Id = "", Name = "未分配" };
listBoxList.Add(person);
listBoxList.Add(new Person() { Id = "a1", Name = "a11" });
listBoxList.Add(new Person() { Id = "a2", Name = "a22" });
//初始化数据
person.Children.Add(new Person() { Id = "", Name = "" });
person.Children.Add(new Person() { Id = "", Name = "" });
person.Children.Add(new Person() { Id = "", Name = "" });
person.Children.Add(new Person() { Id = "", Name = "" });
person.Children.Add(new Person() { Id = "", Name = "" });
person.Children.Add(new Person() { Id = "", Name = "" });
person.Children.Add(new Person() { Id = "", Name = "" });
person.Children.Add(new Person() { Id = "", Name = "" });
person.Children.Add(new Person() { Id = "", Name = "" });
person.Children.Add(new Person() { Id = "", Name = "" }); //控件加载数据
this.m_ListBox.ItemsSource = listBoxList;
}
private void m_DataGrid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Point pos = e.GetPosition(m_DataGrid);
HitTestResult result = VisualTreeHelper.HitTest(m_DataGrid, pos);
DataGrid dataGrid = this.FindVisualParent<DataGrid>(result.VisualHit);
if (dataGrid == null || dataGrid.SelectedItems.Count <= )
{
return;
} List<Person> list = new List<Person>();
for (int i = ; i < dataGrid.SelectedItems.Count; i++)
{
if (dataGrid.SelectedItems[i] is Person)
{
list.Add(dataGrid.SelectedItems[i] as Person);
}
}
DragDrop.DoDragDrop(m_DataGrid, list, DragDropEffects.Move);
}
private void m_ListBox_Drop(object sender, DragEventArgs e)
{
Point pos = e.GetPosition(m_ListBox);
HitTestResult result = VisualTreeHelper.HitTest(m_ListBox, pos);
if (result == null)
{
return;
}
ListBox listBox = this.FindVisualParent<ListBox>(result.VisualHit);
if (listBox == null || listBox.SelectedItem == null)
{
return;
} List<Person> persons = e.Data.GetData(typeof(List<Person>)) as List<Person>;
if (persons != null)
{
foreach (var item in persons)
{
Person person = listBox.SelectedItem as Person;
person.Children.Add(item);
person.ChangeChildCount(); m_SelectedListBoxPerson.Children.Remove(item);
m_SelectedListBoxPerson.ChangeChildCount();
}
}
}
private void m_ListBox_DragOver(object sender, DragEventArgs e)
{
Point pos = e.GetPosition(m_ListBox);
HitTestResult result = VisualTreeHelper.HitTest(m_ListBox, pos);
if (result == null)
{
return;
}
ListBoxItem listBoxItem = this.FindVisualParent<ListBoxItem>(result.VisualHit);
if (listBoxItem == null || listBoxItem.Content == null || !(listBoxItem.Content is Person))
{
return;
} m_ListBox.SelectedItem = listBoxItem.Content;
m_ListBox.Focus();
}
private void m_ListBoxButton_Click(object sender, RoutedEventArgs e)
{
if (m_SelectedListBoxButton != null)
{
m_SelectedListBoxButton.IsEnabled = true;
m_SelectedListBoxButton.Content = "展示";
}
m_SelectedListBoxButton = sender as Button;
m_SelectedListBoxButton.Content = "待分配数据集合";
m_SelectedListBoxButton.IsEnabled = false;
m_ListBox.SelectedItem = m_SelectedListBoxButton.Tag;
m_ListBox.Focus();
m_SelectedListBoxPerson = m_ListBox.SelectedItem as Person;
this.m_DataGrid.ItemsSource = m_SelectedListBoxPerson.Children;
} private T FindVisualParent<T>(DependencyObject obj) where T : class
{
while (obj != null)
{
if (obj is T)
return obj as T;
obj = VisualTreeHelper.GetParent(obj);
}
return null;
}
} class Person : INotifyPropertyChanged
{
public string Id { get; set; }
public string Name { get; set; }
public int ChildCount
{
get {
return this.Children.Count;
}
}
public ObservableCollection<Person> Children { get; set; }
public void ChangeChildCount()
{ this.Changed("ChildCount");
} public Person()
{
this.Children = new ObservableCollection<Person>();
} #region 属性更改通知
public event PropertyChangedEventHandler PropertyChanged;
private void Changed(string PropertyName)
{
if (this .PropertyChanged != null)
this.PropertyChanged(this , new PropertyChangedEventArgs(PropertyName));
}
#endregion
}
}
4、
WPF拖动DataGrid中的数据到ListBox的更多相关文章
- WPF拖动DataGrid滚动条时内容混乱的解决方法
WPF拖动DataGrid滚动条时内容混乱的解决方法 在WPF中,如果DataGrid里使用了模板列,当拖动滚动条时,往往会出现列表内容显示混乱的情况.解决方法就是在Binding的时候给Update ...
- 在WPF的DATAGRID中快速点击出现在ADDNEW或EDITITEM事务过程不允许DEFERREFRESH
原文 在WPF的DATAGRID中快速点击出现在ADDNEW或EDITITEM事务过程不允许DEFERREFRESH 在项目中关于DataGrid的遇到过一些问题,其中是关于迁入CheckBox的双向 ...
- wpf 获取datagrid中模板中控件
//获取name为datagrid中第三列第一行模板的控件 FrameworkElement item = dataGrid.Columns[].GetCellContent(dataGrid.Ite ...
- 在easyui datagrid中formatter数据后使用linkbutton
http://ntzrj513.blog.163.com/blog/static/2794561220139245411997/ formatter:function(value,rowData,ro ...
- [WPF] 在 ViewModel 中让数据验证出错(Validation.HasError)的控件获得焦点
1. 需求 在 MVVM 中 ViewModel 和 View 之间的交互通常都是靠 Icommand 和 INotifyPropertyChanged,不过有时候还会需要从 MVVM 中控制 Vie ...
- 在WPF的DataGrid中对行添加单击事件
在做的一个c#的项目中发现Datagrid没办法直接对鼠标单击进行响应,调用MouseDown事件也需要点击某一行第二次才能响应.所以借助EventSetter来简单的实现了一个. 界面部分的代码 & ...
- wpf mvvm datagrid 中button绑定命令方法
<DataGridTemplateColumn Header="设备状态" IsReadOnly="True" Width="150" ...
- WPF设置DataGrid行内容高度自适应 与 TextBox/TextBlock内容高度自适应
WPF设置DataGrid行内容高度自适应 TextBox/TextBlock内容高度自适应 参考: DataGrid 控件中的调整大小选项: http://msdn.microsoft.com/ ...
- WPF 4 DataGrid 控件(基本功能篇)
原文:WPF 4 DataGrid 控件(基本功能篇) 提到DataGrid 不管是网页还是应用程序开发都会频繁使用.通过它我们可以灵活的在行与列间显示各种数据.本篇将详细介绍WPF 4 中 ...
随机推荐
- http请求中java中的302和sendRedirect的区别
============================================================================================ getCont ...
- 带节日和农历的js日历 带农历的脚本:
<html><head><meta http-equiv="Content-Type" content="text/html; charse ...
- EDE,DEDE网站搬家,DEDECMS搬家教程,一看就会
无聊做个小教程:DEDE搬家,本人提供搬家 先来说下DEDE 大家会安装吧,不多说,看图 一.进入织梦DedeCms的后台备份数据库 步骤:系统 –> 数据库备份/还原 -> 全选有所织梦 ...
- Log4Net IsInfoEnabled 一直 false 的问题
1.概述log4net是.Net下一个非常优秀的开源日志记录组件.log4net记录日志的功能非常强大.它可以将日志分不同的等级,以不同的格式,输出到不同的媒介.本文主要是介绍如何在VisualStu ...
- 九度OJ1081
这道题又一次更新了我的世界观与人生观Orz……最开始我是设计了一个O(n)的递推算法,本以为可以轻松AC没想到居然TLE了……然后搜了一下题解,才发现这道题要用矩阵的思想去做. 通过对题目的分析,我们 ...
- DELPHI SOKET 编程(使用TServerSocket和TClientSocket) 转
http://www.cnblogs.com/findumars/p/5272658.html 本文采用delphi7+TServerSocket+TClientSocket; 笔者在工作中遇到对 ...
- xdg-open 打开“irc:*”链接
用于打开chrome浏览器中的"irc://*" #cp /usr/share/applications/xchat.desktop /home/zsj/.local/share/ ...
- MySQL数据库优化技术概述
对于一个以数据库为中心的应用,数据库的优化直接影响到程序的性能,因此数据库性能至关重要.一般来说,要保证数据库的效率,要做好以下几个方面的工作: 1. 数据库表设计: 表的设计合理化(符合3NF): ...
- 【OpenCV入门教程之三】 图像的载入,显示和输出 一站式完全解析(转)
本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/20537737 作者:毛星云(浅墨) ...
- 004.ASP.NET MVC中的HTML Helpers
原文链接:http://www.codeproject.com/Articles/794579/ASP-NET-MVC-HTML-Helpers-A-MUST-KNOW 1.什么是HTML Helpe ...