Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- foreach (CustomTabItem customTabItem in SelectedWindowsTabControl.Items)
- {
- TabItem ti = tabControl.ItemContainerGenerator.ContainerFromItem(customTabItem) as TabItem;
- Popup popup = (Helpers.FindVisualChild<Popup>(ti) as Popup);
- ImageColorPicker icp = (popup.Child as StackPanel).Children[0] as ImageColorPicker;
- ...
- }
- public class Helpers
- {
- /// <summary>
- /// Return the first visual child of element by type.
- /// </summary>
- /// <typeparam name="T">The type of the Child</typeparam>
- /// <param name="obj">The parent Element</param>
- public static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
- {
- for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
- {
- DependencyObject child = VisualTreeHelper.GetChild(obj, i);
- if (child != null && child is T)
- return (T)child;
- else
- {
- T childOfChild = FindVisualChild<T>(child);
- if (childOfChild != null)
- return childOfChild;
- }
- }
- return null;
- }
- }
- <ControlTemplate TargetType="{x:Type local:CustomTabItem}">
- <Grid Height="26" Background="{TemplateBinding Background}">
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="Auto" />
- <ColumnDefinition Width="Auto" />
- </Grid.ColumnDefinitions>
- <ContentPresenter Margin="5,0" HorizontalAlignment="Left" VerticalAlignment="Center" ContentSource="Header">
- </ContentPresenter>
- <StackPanel Grid.Column="1" Height="16" Margin="0,0,1,0" HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal">
- <ToggleButton x:Name="Edit" Width="16" Content="" Style="{StaticResource CustomizedMetroTabItemToggleButton}" ToolTip="Edit" />
- <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}">
- <StackPanel>
- <local:ImageColorPicker x:Name="ColorPicker" Width="100" Height="100" HorizontalAlignment="Center" Source="Images/ColorWheel.png" />
- </StackPanel>
- </Popup>
- </StackPanel>
- </Grid>
- </ControlTemplate>
- /// <summary>
- /// Finds a Child of a given item in the visual tree.
- /// </summary>
- /// <param name="parent">A direct parent of the queried item.</param>
- /// <typeparam name="T">The type of the queried item.</typeparam>
- /// <param name="childName">x:Name or Name of child. </param>
- /// <returns>The first parent item that matches the submitted type parameter.
- /// If not matching item can be found,
- /// a null parent is being returned.</returns>
- /// <remarks>
- /// http://stackoverflow.com/a/1759923/1188513
- /// </remarks>
- public static T FindChild<T>(this DependencyObject parent, string childName)
- where T : DependencyObject
- {
- if (parent == null) return null;
- T foundChild = null;
- var childrenCount = VisualTreeHelper.GetChildrenCount(parent);
- for (var i = 0; i < childrenCount; i++)
- {
- var child = VisualTreeHelper.GetChild(parent, i);
- var childType = child as T;
- if (childType == null)
- {
- foundChild = FindChild<T>(child, childName);
- if (foundChild != null) break;
- }
- else if (!string.IsNullOrEmpty(childName))
- {
- var frameworkElement = child as FrameworkElement;
- if (frameworkElement != null && frameworkElement.Name == childName)
- {
- foundChild = (T)child;
- break;
- }
- }
- else
- {
- foundChild = (T)child;
- break;
- }
- }
- return foundChild;
- }
- TabItem ti = tabControl.ItemContainerGenerator.ContainerFromItem(customTabItem) as TabItem;
- Popup popup = (Helpers.FindVisualChild<Popup>(ti) as Popup);
- ImageColorPicker icp = (popup.Child as StackPanel).Children[0] as ImageColorPicker;
- var ti = tabControl.ItemContainerGenerator.ContainerFromItem(customTabItem) as TabItem;
- var popup = (Helpers.FindVisualChild<Popup>(ti) as Popup);
- var icp = (popup.Child as StackPanel).Children[0] as ImageColorPicker;
- var tab = tabControl.ItemContainerGenerator.ContainerFromItem(customTabItem) as TabItem;
- var picker = VisualHierarchyHelper.FindChild<ImageColorPicker>(tab, "ColorPicker");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement