Skip to main content
added 14 characters in body
Source Link
Mathieu Guindon
  • 75.1k
  • 18
  • 191
  • 463

I have written the following Codecode to get ImageColorPicker child:

Here's the Templatecontrol template XAML of the TabItem (part of it, the importantthe relevant part):

Is there a better way to get the ImageColorPicker thenthan what I've done? (Gettinggetting the TabItem, then the Popup and then the ImageColorPicker, I am sure there's shortena shorter way)

I have the following Code to get ImageColorPicker child:

Here's the Template of the TabItem (part of it, the important part):

Is there a better way to get the ImageColorPicker then what I've done? (Getting the TabItem, then the Popup and then the ImageColorPicker, I am sure there's shorten)

I have written the following code to get ImageColorPicker child:

Here's the control template XAML of the TabItem (the relevant part):

Is there a better way to get the ImageColorPicker than what I've done? (getting the TabItem, then the Popup and then the ImageColorPicker, I am sure there's a shorter way)

made the title a sentence
Source Link
Malachi
  • 28.7k
  • 11
  • 86
  • 188

Better Is there a better way to get a child?

and here'sHere's the Template of the TabItemTabItem (part of it, the important part):

Better way to get child

and here's the Template of the TabItem (part of it, the important part):

Is there a better way to get a child?

Here's the Template of the TabItem (part of it, the important part):

Source Link
Ron
  • 243
  • 1
  • 2
  • 6

Better way to get child

I have the following Code to get ImageColorPicker child:

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;
    }
}

and here's the Template of the TabItem (part of it, the important part):

<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="&#xE104;" 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>

Is there a better way to get the ImageColorPicker then what I've done? (Getting the TabItem, then the Popup and then the ImageColorPicker, I am sure there's shorten)