1

I have created a WPF app that has navigation between two pages. I want a control(a groupbox) from one of the pages to be hidden by default and to be able to enable it when pressing a keycombo. Home1 is the name of the page and bgdb is the name of the groupbox. Home1_Loaded is hooked up to the page loading inside a frame in MainWindow

public void Home1_Loaded(object sender, RoutedEventArgs e)
{
    bdgb.Visibility = Visibility.Collapsed;
}

What modifications need to be done so I can access bgdb from MainWindow class and unhide it via a keycombo (ex Ctrl+B) ? this is the code for mainwindow loading the homepage by default

private void Window_Initialized(object sender, EventArgs e)
{
    Main.Content = new home();
    Main.Navigate(new Uri("home.xaml", UriKind.RelativeOrAbsolute));
}
mm8
  • 163,881
  • 10
  • 57
  • 88
erma86
  • 75
  • 1
  • 10
  • Look here. This will help you! https://stackoverflow.com/questions/48935/how-can-i-register-a-global-hot-key-to-say-ctrlshiftletter-using-wpf-and-ne – Dev Knight Jan 15 '19 at 13:44

1 Answers1

2

If you are hosting the Page in a Frame element in the MainWindow, you could cast the Content property of the Frame to Home1 and then access any of its members, e.g.:

Home1 home1 = e.Content as Home1;
if (home1 != null)
    home1.bdgb.Visibility = Visibility.Collapsed;

MainWindow.xaml:

<Frame x:Name="frame" />

You could for example handle the Navigated event for the Frame:

private void Window_Initialized(object sender, EventArgs e)
{
    Main.Content = new home();
    Main.Navigated += Main_Navigated;
    Main.Navigate(new Uri("home.xaml", UriKind.RelativeOrAbsolute));
}

private void Main_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
    home home1 = Main.Content as home;
    if (home1 != null)
        home1.bdgb.Visibility = Visibility.Collapsed;
    Main.Navigated -= Main_Navigated;
}
mm8
  • 163,881
  • 10
  • 57
  • 88
  • I have edited a bit the question to include the pageloading code.where would the first part of the code go ? – erma86 Jan 15 '19 at 14:40
  • @erma86: So the `Frame` is named `Main`? See my edit. – mm8 Jan 15 '19 at 14:53
  • Yes.Frame is named Main. Used the edited code but:The type or namespace name 'Home' could not be found (are you missing a using directive or an assembly reference?) error x2 – erma86 Jan 15 '19 at 15:08
  • @erma86: `Home1` is the name of your `Page` class where the `GroupBox` is defined. It should probably be `home` in your case. See my edit. – mm8 Jan 15 '19 at 15:12
  • ok . got it, the class was 'home' in my case. And now for the "reveal" of the hidden object, i could go with protected override bit from here: https://stackoverflow.com/questions/54111705/wpf-detect-key-sequence ? – erma86 Jan 15 '19 at 15:17
  • @erma86: Please don't ask additional questions in the comments field. Ask a new question if you have another issue. This question was about how to access the page from the window. – mm8 Jan 15 '19 at 15:19
  • @erma86: No, it is not. You should only ask one question per thread. – mm8 Jan 15 '19 at 15:22