GridCellSingleClickEditBehavior.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. 
  2. ///////////////////////////////////////////////////////////////////////////////
  3. //Copyright (c) 2016, 北京探索者软件公司
  4. //All rights reserved.
  5. //文件名称: GridCellSingleClickEditBehavior.cs
  6. //文件描述: 窗格单击编辑行为
  7. //创 建 者: xls
  8. //创建日期: 2017-1-11
  9. //版 本 号:1.0.0.0
  10. ////////////////////////////////////////////////////////////////////////////////
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Windows;
  16. using System.Windows.Controls;
  17. using System.Windows.Interactivity;
  18. namespace FWindSoft.Wpf.Behaviors
  19. {
  20. public class GridCellSingleClickEditBehavior : Behavior<UIElement>
  21. {
  22. protected override void OnAttached()
  23. {
  24. base.OnAttached();
  25. this.AssociatedObject.PreviewMouseLeftButtonDown += AssociatedObject_PreviewMouseLeftButtonDown;
  26. }
  27. protected override void OnDetaching()
  28. {
  29. base.OnDetaching();
  30. this.AssociatedObject.PreviewMouseRightButtonDown -= AssociatedObject_PreviewMouseLeftButtonDown;
  31. }
  32. private void AssociatedObject_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
  33. {
  34. UIElement uiel = e.OriginalSource as UIElement;
  35. if (uiel == null)
  36. return;
  37. var cell = uiel.GetParentType<DataGridCell>();
  38. if (cell == null)
  39. return;
  40. if (!cell.IsEditing && !cell.IsReadOnly)
  41. {
  42. if (!cell.IsFocused)
  43. cell.Focus();
  44. DataGrid grid = cell.GetParentType<DataGrid>();
  45. if (grid != null)
  46. {
  47. if (grid.SelectionUnit != DataGridSelectionUnit.FullRow)
  48. {
  49. if (!cell.IsSelected)
  50. {
  51. cell.IsSelected = true;
  52. }
  53. }
  54. else
  55. {
  56. DataGridRow row = cell.GetParentType<DataGridRow>();
  57. if (row != null && !row.IsSelected)
  58. {
  59. row.IsSelected = true;
  60. }
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }