Visual Tree Navigation in WPF

I have an image displayed in a DataTemplate. I want to change the DataTemplate style that surrounds my image when the image is clicked.

It was embedded directly on the stack, as shown below, so I could easily get the parent stack pane by doing the following.

StackPanel sp = img.Parent as StackPanel;
<StackPanel Name="uxSessionImageItem" Style="{DynamicResource RotatorItemTemplateUnselectedStyle}" Loaded="uxSessionImageItem_Loaded" >
     <TextBlock Name="uxLabel"  Width="150" Text="{Binding SessionImageID}" Foreground="White" VerticalAlignment="Center"/>
     <Image MouseDown="ImagePanel_MouseDown" Name="uxImage" Style="{DynamicResource ItemTemplateImageStyle}" Source="{Binding ThumbPath}"/>
</StackPanel>   

I had to add another glass panel and the border between the image and the stack panel that I need to find. What syntax do I need to find, not the border, which is now my parent, or the stack panel, which is the parent border, but the stack panel above this?

<StackPanel Name="uxSessionImageItem" Style="{DynamicResource RotatorItemTemplateUnselectedStyle}" Loaded="uxSessionImageItem_Loaded" >
    <TextBlock Name="uxLabel"  Width="150" Text="{Binding SessionImageID}" VerticalAlignment="Center" Style="{DynamicResource ItemTemplateImageNumberStyle}"/>
    <StackPanel Name="uxSessionImageWrapper" Style="{DynamicResource RotatorItemImageWrapperStyle}" >
        <Border Name="uxImageBorder" Style="{DynamicResource ItemTemplateImageBorderStyle}">
            <Image MouseDown="ImagePanel_MouseDown" Name="uxImage" Style="{DynamicResource ItemTemplateImageStyle}" Source="{Binding ThumbPath}"/>
        </Border>
    </StackPanel>
</StackPanel>
+3
source share
4 answers

VisualTreeHelper . Parent , . .

Linq To Visual Tree Linq , VisualTreeHelper. , , ( ).

+3

CodeNaked , , , , ( , WPF , ?)

public static T FindParentOfType<T>(this DependencyObject child) where T : DependencyObject
{
    DependencyObject parentDepObj = child;
    do
    {
        parentDepObj = VisualTreeHelper.GetParent(parentDepObj);
        T parent = parentDepObj as T;
        if (parent != null) return parent;
    }
    while (parentDepObj != null);
    return null;
}
+1

img.Parent.Parent.Parent - StackPanel.

( , StackPanel, , VisualTree, , .)

: CodeNaked , Parent , , , , .

0

, StackPanel uxSessionImageItem, ?

0
source

All Articles