Advertisement
Guest User

Untitled

a guest
Dec 17th, 2014
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. foreach (CustomTabItem customTabItem in SelectedWindowsTabControl.Items)
  2. {
  3. TabItem ti = tabControl.ItemContainerGenerator.ContainerFromItem(customTabItem) as TabItem;
  4. Popup popup = (Helpers.FindVisualChild<Popup>(ti) as Popup);
  5. ImageColorPicker icp = (popup.Child as StackPanel).Children[0] as ImageColorPicker;
  6.  
  7. ...
  8. }
  9.  
  10. public class Helpers
  11. {
  12. /// <summary>
  13. /// Return the first visual child of element by type.
  14. /// </summary>
  15. /// <typeparam name="T">The type of the Child</typeparam>
  16. /// <param name="obj">The parent Element</param>
  17. public static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
  18. {
  19. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
  20. {
  21. DependencyObject child = VisualTreeHelper.GetChild(obj, i);
  22. if (child != null && child is T)
  23. return (T)child;
  24. else
  25. {
  26. T childOfChild = FindVisualChild<T>(child);
  27. if (childOfChild != null)
  28. return childOfChild;
  29. }
  30. }
  31. return null;
  32. }
  33. }
  34.  
  35. <ControlTemplate TargetType="{x:Type local:CustomTabItem}">
  36. <Grid Height="26" Background="{TemplateBinding Background}">
  37. <Grid.ColumnDefinitions>
  38. <ColumnDefinition Width="Auto" />
  39. <ColumnDefinition Width="Auto" />
  40. </Grid.ColumnDefinitions>
  41. <ContentPresenter Margin="5,0" HorizontalAlignment="Left" VerticalAlignment="Center" ContentSource="Header">
  42. </ContentPresenter>
  43. <StackPanel Grid.Column="1" Height="16" Margin="0,0,1,0" HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal">
  44. <ToggleButton x:Name="Edit" Width="16" Content="&#xE104;" Style="{StaticResource CustomizedMetroTabItemToggleButton}" ToolTip="Edit" />
  45. <Popup HorizontalOffset="{Binding Width, ElementName=Edit}" IsOpen="{Binding IsChecked, Mode=TwoWay, ElementName=Edit}" Placement="Left" PlacementTarget="{Binding ElementName=Edit}" PopupAnimation="Slide" StaysOpen="False" VerticalOffset="{Binding ActualHeight, ElementName=Edit}">
  46. <StackPanel>
  47. <local:ImageColorPicker x:Name="ColorPicker" Width="100" Height="100" HorizontalAlignment="Center" Source="Images/ColorWheel.png" />
  48. </StackPanel>
  49. </Popup>
  50. </StackPanel>
  51. </Grid>
  52. </ControlTemplate>
  53.  
  54. /// <summary>
  55. /// Finds a Child of a given item in the visual tree.
  56. /// </summary>
  57. /// <param name="parent">A direct parent of the queried item.</param>
  58. /// <typeparam name="T">The type of the queried item.</typeparam>
  59. /// <param name="childName">x:Name or Name of child. </param>
  60. /// <returns>The first parent item that matches the submitted type parameter.
  61. /// If not matching item can be found,
  62. /// a null parent is being returned.</returns>
  63. /// <remarks>
  64. /// http://stackoverflow.com/a/1759923/1188513
  65. /// </remarks>
  66. public static T FindChild<T>(this DependencyObject parent, string childName)
  67. where T : DependencyObject
  68. {
  69. if (parent == null) return null;
  70.  
  71. T foundChild = null;
  72.  
  73. var childrenCount = VisualTreeHelper.GetChildrenCount(parent);
  74. for (var i = 0; i < childrenCount; i++)
  75. {
  76. var child = VisualTreeHelper.GetChild(parent, i);
  77. var childType = child as T;
  78. if (childType == null)
  79. {
  80. foundChild = FindChild<T>(child, childName);
  81. if (foundChild != null) break;
  82. }
  83. else if (!string.IsNullOrEmpty(childName))
  84. {
  85. var frameworkElement = child as FrameworkElement;
  86. if (frameworkElement != null && frameworkElement.Name == childName)
  87. {
  88. foundChild = (T)child;
  89. break;
  90. }
  91. }
  92. else
  93. {
  94. foundChild = (T)child;
  95. break;
  96. }
  97. }
  98.  
  99. return foundChild;
  100. }
  101.  
  102. TabItem ti = tabControl.ItemContainerGenerator.ContainerFromItem(customTabItem) as TabItem;
  103. Popup popup = (Helpers.FindVisualChild<Popup>(ti) as Popup);
  104. ImageColorPicker icp = (popup.Child as StackPanel).Children[0] as ImageColorPicker;
  105.  
  106. var ti = tabControl.ItemContainerGenerator.ContainerFromItem(customTabItem) as TabItem;
  107. var popup = (Helpers.FindVisualChild<Popup>(ti) as Popup);
  108. var icp = (popup.Child as StackPanel).Children[0] as ImageColorPicker;
  109.  
  110. var tab = tabControl.ItemContainerGenerator.ContainerFromItem(customTabItem) as TabItem;
  111. var picker = VisualHierarchyHelper.FindChild<ImageColorPicker>(tab, "ColorPicker");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement