Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get parent of an element?

MainPage.xaml

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid x:Name="GridA">
        <Grid x:Name="GridB"/>
    </Grid>
</Grid>

It is possible get GridB parent from GridB

This is what I'm trying to do

//Null
Panel parent1 = GridB.Parent as Panel;

//Null
Panel parent2 = VisualTreeHelper.GetParent(GridB) as Panel;

All of them return null.

Any idea?

like image 883
aiur Avatar asked Jan 06 '23 04:01

aiur


1 Answers

Use VisualTreeHelper.GetParent method but as UIElement not Panel like this:

var parent = VisualTreeHelper.GetParent(GridB) as UIElement;
string pName = (parent as Grid).Name; //GridA
like image 192
Salah Akbari Avatar answered Jan 21 '23 23:01

Salah Akbari