Search code examples
c#wpfpixelsense

WPF: Calling a method from a different "branch" of the tree


Hey, I'm doing a WPF Application.

The tree looks like this:

SurfaceWindow --- Startscreen
..........................-------- Page---------- Subpage

I'm trying to call a method from the "Subpage" from the "Code Behind" of the Startscreen(Startscreen.xaml.cs).

The method from the Subpage looks like this:

public void showTheme(ThemeViewModel theme) { ... }

If know that I can call it when I'm on the "Page" or the "SurfaceWindow", because it's in the same "branch" of the tree, and I just do something like this:

        ThemeViewModel theme = (ThemeViewModel)mvm.CurrentItem.ThemeViewModel;
        katalog.katalogblatt.showTheme(theme);

But how do I do it when I'm not on the same branch of the tree and want to call the method?


Solution

  • Can you provide more detail? your Terminology can mean various things. Providing you mean you want to call a method called 'showTheme' on a control called 'katalogBlatt' outside of the parent control then you either need to expose a method on the parent to call the child controls method:

    public void ShowTheme(ThemeViewModel theme)
    {
       this.katalogBlatt.ShowTheme(theme)
    }
    

    and call it as:

    page.ShowTheme(theme)
    

    or you need to expose the control as public property on the parent:

    public <controlType> Catalog
    {
       get
       {
          return katalogBlatt;
       }
    }
    

    and call it as:

    page.Catalog.ShowTheme(theme);