1、效果图:

2、XAML

  1. <Window x:Class="WpfApplication2.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
  5. <Grid>
  6. <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">
  7. <ListBox.ItemTemplate>
  8. <DataTemplate>
  9. <StackPanel>
  10. <TextBlock Text="{Binding Path=Id, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
  11. <TextBlock Text="{Binding Path=Name, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
  12. <TextBlock Text="{Binding Path=ChildCount, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
  13. <Button Content="展示" Tag="{Binding}" Name="m_ListBoxButton" Click="m_ListBoxButton_Click" />
  14. </StackPanel>
  15. </DataTemplate>
  16. </ListBox.ItemTemplate>
  17. </ListBox>
  18. <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">
  19. <DataGrid.Columns>
  20. <DataGridTextColumn Binding="{Binding Id}" IsReadOnly="True" />
  21. <DataGridTextColumn Binding="{Binding Name}" IsReadOnly="True" />
  22. </DataGrid.Columns>
  23. </DataGrid>
  24. </Grid>
  25. </Window>

3、CS

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using System.Collections.ObjectModel;
  15. using System.ComponentModel;
  16.  
  17. namespace WpfApplication2
  18. {
  19. /// <summary>
  20. /// MainWindow.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class MainWindow : Window
  23. {
  24. private ObservableCollection<Person> listBoxList = new ObservableCollection<Person>();
  25. private Person m_SelectedListBoxPerson;
  26. private Button m_SelectedListBoxButton;
  27.  
  28. public MainWindow()
  29. {
  30. InitializeComponent();
  31. }
  32.  
  33. private void Window_Loaded(object sender, RoutedEventArgs e)
  34. {
  35. Person person = new Person() { Id = "", Name = "未分配" };
  36. listBoxList.Add(person);
  37. listBoxList.Add(new Person() { Id = "a1", Name = "a11" });
  38. listBoxList.Add(new Person() { Id = "a2", Name = "a22" });
  39. //初始化数据
  40. person.Children.Add(new Person() { Id = "", Name = "" });
  41. person.Children.Add(new Person() { Id = "", Name = "" });
  42. person.Children.Add(new Person() { Id = "", Name = "" });
  43. person.Children.Add(new Person() { Id = "", Name = "" });
  44. person.Children.Add(new Person() { Id = "", Name = "" });
  45. person.Children.Add(new Person() { Id = "", Name = "" });
  46. person.Children.Add(new Person() { Id = "", Name = "" });
  47. person.Children.Add(new Person() { Id = "", Name = "" });
  48. person.Children.Add(new Person() { Id = "", Name = "" });
  49. person.Children.Add(new Person() { Id = "", Name = "" });
  50.  
  51. //控件加载数据
  52. this.m_ListBox.ItemsSource = listBoxList;
  53. }
  54. private void m_DataGrid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  55. {
  56. Point pos = e.GetPosition(m_DataGrid);
  57. HitTestResult result = VisualTreeHelper.HitTest(m_DataGrid, pos);
  58. DataGrid dataGrid = this.FindVisualParent<DataGrid>(result.VisualHit);
  59. if (dataGrid == null || dataGrid.SelectedItems.Count <= )
  60. {
  61. return;
  62. }
  63.  
  64. List<Person> list = new List<Person>();
  65. for (int i = ; i < dataGrid.SelectedItems.Count; i++)
  66. {
  67. if (dataGrid.SelectedItems[i] is Person)
  68. {
  69. list.Add(dataGrid.SelectedItems[i] as Person);
  70. }
  71. }
  72. DragDrop.DoDragDrop(m_DataGrid, list, DragDropEffects.Move);
  73. }
  74. private void m_ListBox_Drop(object sender, DragEventArgs e)
  75. {
  76. Point pos = e.GetPosition(m_ListBox);
  77. HitTestResult result = VisualTreeHelper.HitTest(m_ListBox, pos);
  78. if (result == null)
  79. {
  80. return;
  81. }
  82. ListBox listBox = this.FindVisualParent<ListBox>(result.VisualHit);
  83. if (listBox == null || listBox.SelectedItem == null)
  84. {
  85. return;
  86. }
  87.  
  88. List<Person> persons = e.Data.GetData(typeof(List<Person>)) as List<Person>;
  89. if (persons != null)
  90. {
  91. foreach (var item in persons)
  92. {
  93. Person person = listBox.SelectedItem as Person;
  94. person.Children.Add(item);
  95. person.ChangeChildCount();
  96.  
  97. m_SelectedListBoxPerson.Children.Remove(item);
  98. m_SelectedListBoxPerson.ChangeChildCount();
  99. }
  100. }
  101. }
  102. private void m_ListBox_DragOver(object sender, DragEventArgs e)
  103. {
  104. Point pos = e.GetPosition(m_ListBox);
  105. HitTestResult result = VisualTreeHelper.HitTest(m_ListBox, pos);
  106. if (result == null)
  107. {
  108. return;
  109. }
  110. ListBoxItem listBoxItem = this.FindVisualParent<ListBoxItem>(result.VisualHit);
  111. if (listBoxItem == null || listBoxItem.Content == null || !(listBoxItem.Content is Person))
  112. {
  113. return;
  114. }
  115.  
  116. m_ListBox.SelectedItem = listBoxItem.Content;
  117. m_ListBox.Focus();
  118. }
  119. private void m_ListBoxButton_Click(object sender, RoutedEventArgs e)
  120. {
  121. if (m_SelectedListBoxButton != null)
  122. {
  123. m_SelectedListBoxButton.IsEnabled = true;
  124. m_SelectedListBoxButton.Content = "展示";
  125. }
  126. m_SelectedListBoxButton = sender as Button;
  127. m_SelectedListBoxButton.Content = "待分配数据集合";
  128. m_SelectedListBoxButton.IsEnabled = false;
  129. m_ListBox.SelectedItem = m_SelectedListBoxButton.Tag;
  130. m_ListBox.Focus();
  131. m_SelectedListBoxPerson = m_ListBox.SelectedItem as Person;
  132. this.m_DataGrid.ItemsSource = m_SelectedListBoxPerson.Children;
  133. }
  134.  
  135. private T FindVisualParent<T>(DependencyObject obj) where T : class
  136. {
  137. while (obj != null)
  138. {
  139. if (obj is T)
  140. return obj as T;
  141. obj = VisualTreeHelper.GetParent(obj);
  142. }
  143. return null;
  144. }
  145. }
  146.  
  147. class Person : INotifyPropertyChanged
  148. {
  149. public string Id { get; set; }
  150. public string Name { get; set; }
  151. public int ChildCount
  152. {
  153. get {
  154. return this.Children.Count;
  155. }
  156. }
  157. public ObservableCollection<Person> Children { get; set; }
  158. public void ChangeChildCount()
  159. {
  160.  
  161. this.Changed("ChildCount");
  162. }
  163.  
  164. public Person()
  165. {
  166. this.Children = new ObservableCollection<Person>();
  167. }
  168.  
  169. #region 属性更改通知
  170. public event PropertyChangedEventHandler PropertyChanged;
  171. private void Changed(string PropertyName)
  172. {
  173. if (this .PropertyChanged != null)
  174. this.PropertyChanged(this , new PropertyChangedEventArgs(PropertyName));
  175. }
  176. #endregion
  177. }
  178. }

4、

WPF拖动DataGrid中的数据到ListBox的更多相关文章

  1. WPF拖动DataGrid滚动条时内容混乱的解决方法

    WPF拖动DataGrid滚动条时内容混乱的解决方法 在WPF中,如果DataGrid里使用了模板列,当拖动滚动条时,往往会出现列表内容显示混乱的情况.解决方法就是在Binding的时候给Update ...

  2. 在WPF的DATAGRID中快速点击出现在ADDNEW或EDITITEM事务过程不允许DEFERREFRESH

    原文 在WPF的DATAGRID中快速点击出现在ADDNEW或EDITITEM事务过程不允许DEFERREFRESH 在项目中关于DataGrid的遇到过一些问题,其中是关于迁入CheckBox的双向 ...

  3. wpf 获取datagrid中模板中控件

    //获取name为datagrid中第三列第一行模板的控件 FrameworkElement item = dataGrid.Columns[].GetCellContent(dataGrid.Ite ...

  4. 在easyui datagrid中formatter数据后使用linkbutton

    http://ntzrj513.blog.163.com/blog/static/2794561220139245411997/ formatter:function(value,rowData,ro ...

  5. [WPF] 在 ViewModel 中让数据验证出错(Validation.HasError)的控件获得焦点

    1. 需求 在 MVVM 中 ViewModel 和 View 之间的交互通常都是靠 Icommand 和 INotifyPropertyChanged,不过有时候还会需要从 MVVM 中控制 Vie ...

  6. 在WPF的DataGrid中对行添加单击事件

    在做的一个c#的项目中发现Datagrid没办法直接对鼠标单击进行响应,调用MouseDown事件也需要点击某一行第二次才能响应.所以借助EventSetter来简单的实现了一个. 界面部分的代码 & ...

  7. wpf mvvm datagrid 中button绑定命令方法

    <DataGridTemplateColumn Header="设备状态" IsReadOnly="True" Width="150" ...

  8. WPF设置DataGrid行内容高度自适应 与 TextBox/TextBlock内容高度自适应

    WPF设置DataGrid行内容高度自适应  TextBox/TextBlock内容高度自适应  参考: DataGrid 控件中的调整大小选项: http://msdn.microsoft.com/ ...

  9. WPF 4 DataGrid 控件(基本功能篇)

    原文:WPF 4 DataGrid 控件(基本功能篇)      提到DataGrid 不管是网页还是应用程序开发都会频繁使用.通过它我们可以灵活的在行与列间显示各种数据.本篇将详细介绍WPF 4 中 ...

随机推荐

  1. Windows2008防火墙封ip

    http://www.bitscn.com/os/windows/201411/406212.html

  2. 前端实现 SVG 转 PNG

    http://fex.baidu.com/blog/2015/11/convert-svg-to-png-at-frontend/ 前言 svg 是一种矢量图形,在 web 上应用很广泛,但是很多时候 ...

  3. Python语言快速入门

    Python的主提示符(>>>):是解释器告诉你它正在等待你输入的下一个语句 Python的次提示符(...):告诉你解释器正在等待你输入当前语句的其他部分 [简介] Python( ...

  4. Salted Password Hashing

    Here are some examples of poor wacky hash functions I've seen suggested in forums on the internet. m ...

  5. PHP 连接 MSSQL

    1 设置 2 php 代码: <?php header('Content-Type:text/html; charset=GBK'); define('DB_HOST','localhost') ...

  6. 字典查找、linq、foreach、yield等几种查找性能对比

    先上代码,以1千万记录的内存查找测试: List<Student> stuList = new List<Student>(); Dictionary<int, Stud ...

  7. 立体匹配:关于Middlebury提供的源码的简化使用

    Middlebury提供的源码,虽然花了不到一个小时就运行起来啦.但说实话,它那循环读取脚本命令来执行算法真是让我费了不少头脑,花了近三天时间,我才弄明白了它的运行机制.你说,我就想提取一下算法,你给 ...

  8. 监控RAC中的临时表空间

    it is from metalink:Note:465840.1 1>Monitor the temp space allocation to make sure each instance ...

  9. jdk线程的生产者消费者问题

    同步代码块实现生产者消费者模式 class Person { private String name; private String sex; private Boolean isEmpty = Bo ...

  10. Metrics.NET report to Zabbix

    废话不多说,先上git地址 https://github.com/binking338/Metrics.Reporters.ZabbixReporter 实现了Metrics.NET到Zabbix的报 ...