user1841243
user1841243

Reputation: 1713

how does a custom control in a usercontrol find the parent of the usercontrol in WPF?

I have a custom control that is used in a usercontrol, and this usercontrol is hosted in a MainUsercontrol.

now, when an event occurs in the custom control, the selected tab in the MainUsercontrol should change.

Is there a way to say something like the following??

this.Parent.Parent.SelectTab(1);  

?

Upvotes: 0

Views: 164

Answers (2)

user1841243
user1841243

Reputation: 1713

Thanks to all answers... I found the below solution, but i'll look in to the solutions presented here above as well....

First Get the Parent Window WPF User Control Parent

Then foreach for the Usercontrol and then Foreach again to find the Tabcontrol Find all controls in WPF Window by type

Upvotes: 0

Viv
Viv

Reputation: 17380

Changing such stuff on a parent control directly from a child is generally frowned upon.

Why don't you make this event in your child control a bubbling RoutedEvent to let the parent in the visual tree handle the event and action accordingly as it see's fit.

Some helpful links:

How to: Create a Custom Routed Event

Extensive - Routed Events Overview

Sample:

A demo showing your use case where the MainWindow holds a UserControl which holds another UserControl within it and the MainWindow handles the custom event raised by the grand child UserControl.

Download Link

Upvotes: 1

Related Questions