Advertisement
parabola949

Untitled

Feb 17th, 2014
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.25 KB | None | 0 0
  1. //DISCLAIMER:  This code was copied from an article, and is by no means how I would code.  
  2. //I am ashamed to even have this in my pastebin...
  3.  
  4. private void ToggleButton_Click(object sender, RoutedEventArgs e)
  5.         {
  6.             DependencyObject obj = (DependencyObject)e.OriginalSource;
  7.             while (!(obj is DataGridRow) && obj != null) obj = VisualTreeHelper.GetParent(obj);
  8.             if (obj is DataGridRow)
  9.             {
  10.                 if ((obj as DataGridRow).DetailsVisibility == Visibility.Visible)
  11.                 {
  12.                     (obj as DataGridRow).IsSelected = false;
  13.                 }
  14.                 else
  15.                 {
  16.                     (obj as DataGridRow).IsSelected = true;
  17.                 }
  18.             }
  19.         }
  20.  
  21.         public static FrameworkElement GetTemplateChildByName(DependencyObject parent, string name)
  22.         {
  23.             int childnum = VisualTreeHelper.GetChildrenCount(parent);
  24.             for (int i = 0; i < childnum; i++)
  25.             {
  26.                 var child = VisualTreeHelper.GetChild(parent, i);
  27.                 if (child is FrameworkElement && ((FrameworkElement)child).Name == name)
  28.                 {
  29.                     return child as FrameworkElement;
  30.                 }
  31.                 else
  32.                 {
  33.                     var s = GetTemplateChildByName(child, name);
  34.                     if (s != null)
  35.                         return s;
  36.                 }
  37.             }
  38.             return null;
  39.         }
  40.  
  41.         private void dataGrid1_RowDetailsVisibilityChanged(object sender, DataGridRowDetailsEventArgs e)
  42.         {
  43.             DataGridRow row = e.Row as DataGridRow;
  44.             FrameworkElement tb = GetTemplateChildByName(row, "RowHeaderToggleButton");
  45.             if (tb != null)
  46.             {
  47.                 if (row.DetailsVisibility == System.Windows.Visibility.Visible)
  48.                 {
  49.                     (tb as ToggleButton).IsChecked = true;
  50.                 }
  51.                 else
  52.                 {
  53.                     (tb as ToggleButton).IsChecked = false;
  54.                 }
  55.             }
  56.  
  57.         }
  58.  
  59.  
  60. //DISCLAIMER:  This code was copied from an article, and is by no means how I would code.  
  61. //I am ashamed to even have this in my pastebin...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement