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>
source
share