0

I have developed a WPF application in which has a stack panel with four buttons that acts as a navigation system. The buttons correspond to a window. E.g About, Update, Rename and Exit. So when my application loads the AboutWindow is opened. user can then select any button and the application will show that Window and close the current window.

XAML - AboutWindow.xaml

<StackPanel>
    <Button Name="AboutBtn" Click="AboutBtnNavigate">About</Button>
    <Button Name="UpdateBtn" Click="UpdateBtnNavigate">About</Button>
    <Button Name="RenameBtn" Click="RenameBtnNavigate">About</Button>
    <Button Name="ExitBtn" Click="ExitBtnNavigate">About</Button>
</StackPanel>

C# - AboutWindow.xaml.cs (class AboutWindow)

private void AboutBtnNavigate(object sender, RoutedEventArgs e)
{
    this.Show()
}

private void UpdateBtnNavigate(object sender, RoutedEventArgs e)
{
    UpdateWindow updateWindow = new UpdateWindow();
    updateWindow.Show();
    this.Close();
}

private void RenameBtnNavigate(object sender, RoutedEventArgs e)
{
    RenameWindow renameWindow = new RenameWindow();
    renameWindow .Show();
    this.Close();
}

private void ExitBtnNavigate(object sender, RoutedEventArgs e)
{
     ExitWindow exitWindow = new ExitWindow ();
     exitWindow .ShowDialog();

}

So currently these event handlers are in the AboutWindow class but I will need to use the same methods elsewhere, for example in UpdateWindow class and RenameWindow Class. Is there away to reuse these methods without having to rewrite the code, bearing in mind the code for each method will slightly change depending on the class they are in. Eg. if used in UpdateWindow, UpdateBtnNavigate will actually be just Show();.

Essentially I am asking whats the best way to use OOP to limit the amount of repeating code in this situation. Hopefully this makes sense. Thanks in advance!

  • 1
    Typically in xaml you bind your buttons to an ICommand. You can use that same command in every window. Side note, did you know WPF has a navigation system? – Crowcoder Apr 04 '20 at 11:45
  • you dont have to use multiple windows. you can go with mvvm and datatemplates for your different viewmodels have a look here: https://stackoverflow.com/questions/19654295/wpf-mvvm-navigate-views – Bobbey Apr 04 '20 at 11:53
  • Several things to consider (without MVVM) ! No 1 : you can have just 1 main Window in which These Btns loaded and all other windows will be UserControls OR Pages. so that you will stay on same window always and only Relevant pages or UserControls will change when clicking any btn – Hammas Apr 04 '20 at 12:05
  • 2 : If you always want WINDOWS then you just create one UserControl which contains all these Btns and you will add this UserControl in each Window and boom (you will have same Uc being used everywhere) – Hammas Apr 04 '20 at 12:06
  • @Crowcoder. Thanks this is really useful, I didn't know there was navigation in WPF. –  Apr 04 '20 at 12:15
  • @Bobbey Thanks I have a look a viewmodels –  Apr 04 '20 at 12:16
  • 1
    @RaoHammasHussain THanks for the coments. Realy useful I have a think about all WIndows, main Window plus pages or as other comments suggested viewmodels. –  Apr 04 '20 at 12:17

0 Answers0