Advertisement
Guest User

Untitled

a guest
May 30th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. public class DgColumnBehavior : Behavior<DataGrid>
  2. {
  3. protected ICommand ToggleColumnVisibilityCmd;
  4. protected DataGrid _AssociatedObject;
  5. private static VisibilityToBooleanConverter visibilityToBooleanConverter = new VisibilityToBooleanConverter() { HiddenState = Visibility.Hidden };
  6.  
  7. protected override void OnAttached()
  8. {
  9. this.ToggleColumnVisibilityCmd = new DelegateCommand<DataGridColumn>(ToggleColumnVisibilityCmdExecute);
  10. this._AssociatedObject = (DataGrid)this.AssociatedObject;
  11.  
  12. Observable.FromEventPattern(this._AssociatedObject, "Loaded")
  13. .Take(1)
  14. .Subscribe(x => _AssociatedObject_Loaded());
  15.  
  16. base.OnAttached();
  17. }
  18.  
  19. void _AssociatedObject_Loaded()
  20. {
  21. var columnHeaders = this._AssociatedObject.SafeFindDescendants<DataGridColumnHeader>(); // see second code piece for the SafeFindDescendants extension method
  22.  
  23. foreach (var columnHeader in columnHeaders)
  24. {
  25. EnsureSeparateContextMenuFor(columnHeader);
  26.  
  27. if (columnHeader.ContextMenu.ItemsSource != null)
  28. {
  29. // ContextMenu has an ItemsSource, so need to add items to that -
  30. // ommitted though as irrelevant for example
  31. }
  32. else
  33. {
  34. // No ItemsSource assigned to the Menu, so we can just add directly
  35.  
  36. foreach (var item in CreateMenuItemsFor(columnHeader))
  37. columnHeader.ContextMenu.Items.Add(item);
  38. }
  39. }
  40. }
  41.  
  42. /// Ensures that the columnHeader ...
  43. /// A) has a ContextMenu, and
  44. /// B) that is has an individual context menu, i.e. one that isn't shared with any other DataGridColumnHeaders.
  45. ///
  46. /// I'm doing that as in practice, I'm adding some further items that are specific to each column, so I can't have a shared context menu
  47. private void EnsureSeparateContextMenuFor(DataGridColumnHeader columnHeader)
  48. {
  49. if (columnHeader.ContextMenu == null)
  50. {
  51. columnHeader.ContextMenu = new ContextMenu();
  52. }
  53. else
  54. {
  55. // clone the existing menu
  56. // ommitted as irrelevant for example
  57. }
  58. }
  59.  
  60. /// Creates one menu item for each column of the underlying DataGrid to toggle that column's visibility
  61. private IEnumerable<FrameworkElement> CreateMenuItemsFor(DataGridColumnHeader columnHeader)
  62. {
  63. foreach (var column in _AssociatedObject.Columns)
  64. {
  65. var item = new MenuItem();
  66. item.Header = String.Format("Toggle visibility for {0}", column.Header);
  67. item.Command = ToggleColumnVisibilityCmd;
  68. item.CommandParameter = column;
  69.  
  70. yield return item;
  71. }
  72. }
  73.  
  74. // Gets executed when the user clicks on one of the ContextMenu items
  75. protected void ToggleColumnVisibilityCmdExecute(DataGridColumn column)
  76. {
  77. bool isVisible = (column.Visibility == Visibility.Visible);
  78. Visibility newVisibility = (isVisible) ? Visibility.Hidden : Visibility.Visible;
  79. column.Visibility = newVisibility;
  80. }
  81. }
  82.  
  83. public static class Visual_ExtensionMethods
  84. {
  85. public static IEnumerable<T> SafeFindDescendants<T>(this Visual @this, Predicate<T> predicate = null) where T : Visual
  86. {
  87. if (@this != null)
  88. {
  89. int childrenCount = VisualTreeHelper.GetChildrenCount(@this);
  90. for (int i = 0; i < childrenCount; i++)
  91. {
  92. var currentChild = VisualTreeHelper.GetChild(@this, i);
  93.  
  94. var typedChild = currentChild as T;
  95. if (typedChild == null)
  96. {
  97. var result = ((Visual)currentChild).SafeFindDescendants<T>(predicate);
  98.  
  99. foreach (var r in result)
  100. yield return r;
  101.  
  102. }
  103. else
  104. {
  105. if (predicate == null || predicate(typedChild))
  106. {
  107. yield return typedChild;
  108. }
  109. }
  110. }
  111.  
  112. }
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement