简体   繁体   中英

How do I get the grid parent of my control?

I have a control in a grid and would like to access the grid. I don't have access to the grid in the code behind but I do have access to an instance of the control. Is there a way to obtain the grid parent using the instance of the control?

Assuming that your control is a button within a Grid, and that you access to this Button via the clicking event, this is how you would access the Grid.

private void button1_Click(object sender, RoutedEventArgs e)
{
    Grid x = (sender as Button).Parent as Grid;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM