DotNetRussell
DotNetRussell

Reputation: 9867

Get the name of the parent user control WPF C#

I have access to User Control A. I want to get info on User Control C. Is there any way to do this in WPF? The structure is basically what you see. User Control D is a ribbon, C is a tab on the ribbon and B and A are contents of C. I can't seem to get access to C. I tried using the Parent property of A but it doesn't seem to give me the info on C.

enter image description here

Upvotes: 22

Views: 26940

Answers (3)

Mehdi Benkirane
Mehdi Benkirane

Reputation: 445

use the Window.GetWindow(this) method within Loaded event handler.

public MainView()
{
    InitializeComponent();

    this.Loaded += new RoutedEventHandler(MainView_Loaded);
}

void MainView_Loaded(object sender, RoutedEventArgs e)
{
    Window parentWindow = Window.GetWindow(this);

    ...
}

Upvotes: -1

Guilherme H. J.
Guilherme H. J.

Reputation: 89

Maybe you can try to cast the parent as UserControl C, like this:

(this.Parent as UserControlC).YourProperty

Upvotes: 4

Rwiti
Rwiti

Reputation: 1066

Try using VisualTreeHelper.GetParent or use the recursive function here

Upvotes: 18

Related Questions