How to find children of UserControl instead of Window - replacing Window.FindName

I currently have a WPF project that has one main window and many UserControls that are children of this window. Many of the children in this window are tabs. I have successfully replaced my main window with User Control, which implements almost the same functionality as the main window.

Replacing a window using UserControl introduced one problem - our application currently determines which programming tab to display based on the parent window using the method Window.FindNameshown below. Therefore, I need to replace Application.Current.MainWindowwith the corresponding description of my main user control. See the following C # method with an error and instantiating the wpf tab for an explanation.

Note. As for the Window.FindName () method , the reason it doesn't work after I replaced it with UserControl is because the FindName method is looking up in the visual tree, as described here .

Does anyone know how to find a x: Name based user control that is similar Application.Current.MainWindow? Also, is there a better way to find UserControl than looking for the x: Name string if it is renamed?

As we find MainWindow now - now we need to find MainUserControl:

(C#)
private static void SetCurrentProgram(int num)
    {
        Window window = Application.Current.MainWindow;
        ProgrammingTab programmingTab1 = window.FindName("ProgrammingTab1") as ProgrammingTab;
        ProgrammingTab programmingTab2 = window.FindName("ProgrammingTab2") as ProgrammingTab;

        programmingTab1.Visibility = num == 1 ? Visibility.Visible : Visibility.Collapsed;
        programmingTab2.Visibility = num == 2 ? Visibility.Visible : Visibility.Collapsed;

    }

Activate programming tabs.

(xaml)
<Grid>
 <ProgrammingControl:ProgrammingTab x:Name="ProgrammingTab1" Program="1" IsVisibleChanged="ProgrammingTab_IsVisibleChanged" />
 <ProgrammingControl:ProgrammingTab x:Name="ProgrammingTab2" Program="2" IsVisibleChanged="ProgrammingTab_IsVisibleChanged" />
</Grid>
+3
source share
3 answers

: , StackOverflow, DependencyObject. . FindChild() public static class UIHelper {}, , :

  ProgrammingTab programmingTab1 = UIHelper.FindChild<ProgrammingTab>(Application.Current.MainWindow, "ProgrammingTab1");
  ProgrammingTab programmingTab2 = UIHelper.FindChild<ProgrammingTab>(Application.Current.MainWindow, "ProgrammingTab2");

XAML , , RayBurns. , , , . , .

, FindName() , .

0

, WinForms. , FindName() UserControl ProgrammingTab, :

var userControl = (MyUserControl)Application.Current.MainWindow.FindName("userControlName");
var programmingTab1 = (ProgrammingTab)userControl.FindName("ProgrammingTab1");
var programmingTab2 = (ProgrammingTab)userControl.FindName("ProgrammingTab2");
...

WPF, . DependencyProperty "CurrentProgram" singleton, , Visiblity .

<ProgrammingTab Visibilty="{Binding CurrentProgram,
   Source={x:Static MyState.Instance},
   Converter={x:Static VisibleIfEqualConverter},
   ConverterParameter=1}" ...>
  ...

SetCurrentProgram :

public void SetCurrentProgram(int num)
{
   MyState.Instance.CurrentProgram = num;
}

, ProgrammingTab , MyState.Instance.CurrentProgram vaulue, FindName() .

+2

Source: https://habr.com/ru/post/1723268/


All Articles