Skip to content
This repository was archived by the owner on Jan 21, 2022. It is now read-only.

Commit 3144246

Browse files
authored
Merge pull request #10 from Standaard-boos/Navigation
Make navigation MVVM compliant
2 parents c8b71a8 + c63f608 commit 3144246

5 files changed

Lines changed: 195 additions & 80 deletions

File tree

View/MainMenuData.cs

Lines changed: 0 additions & 55 deletions
This file was deleted.

View/MainWindow.xaml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
55
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
66
xmlns:local="clr-namespace:View"
7+
xmlns:vm="clr-namespace:ViewModel;assembly=ViewModel"
8+
xmlns:vmc="clr-namespace:ViewModel.Commands;assembly=ViewModel"
79
mc:Ignorable="d"
8-
Title="MainWindow" Height="450" Width="800">
10+
Title="Stugether" Height="450" Width="800">
11+
<Window.DataContext>
12+
<vm:MainPageViewModel/>
13+
</Window.DataContext>
914
<DockPanel LastChildFill="True">
10-
<DockPanel.DataContext>
11-
<local:MainMenuData/>
12-
</DockPanel.DataContext>
1315
<StackPanel DockPanel.Dock="Left" Width="100">
1416
<Grid Height="{Binding ActualHeight, ElementName=tbContentTitle, Mode=OneWay}">
1517
<Grid.ColumnDefinitions>
@@ -28,12 +30,12 @@
2830
</ItemsControl.ItemsPanel>
2931
<ItemsControl.ItemTemplate>
3032
<ItemContainerTemplate>
31-
<local:MainWindowNavigationItem Content="{Binding Title}" Page="{Binding Page}" ExtraNavigationInformation="{Binding ExtraInformation}" Click="MainWindowNavigationItem_Click"/>
33+
<local:MainWindowNavigationItem Content="{Binding Title}" Page="{Binding Page}" ExtraNavigationInformation="{Binding ExtraInformation}" Command="{Binding Path=DataContext.NavigateButtonCommand, RelativeSource={RelativeSource AncestorType=Window},Mode=Default}" CommandParameter="{Binding Mode=OneWay}"/>
3234
</ItemContainerTemplate>
3335
</ItemsControl.ItemTemplate>
3436
</ItemsControl>
3537
</StackPanel>
36-
<TextBlock x:Name="tbContentTitle" DockPanel.Dock="Top" Text="Title" FontSize="22" FontWeight="Bold"/>
37-
<Frame x:Name="frContent" NavigationUIVisibility="Hidden" Navigated="frContent_Navigated"/>
38+
<TextBlock x:Name="tbContentTitle" DockPanel.Dock="Top" FontSize="22" FontWeight="Bold" Text="{Binding Title}"/>
39+
<Frame x:Name="frContent" NavigationUIVisibility="Hidden" Navigated="frContent_Navigated" Source="{Binding CurrentVisiblePage}" />
3840
</DockPanel>
3941
</Window>

View/MainWindow.xaml.cs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,9 @@ public partial class MainWindow : Window
1414
public MainWindow()
1515
{
1616
InitializeComponent();
17-
SSHConnection.InitializeSsh(); // Not MVVM, this needs to be moved somewhere soon
17+
SSHConnection.InitializeSsh(); // TODO: Not MVVM, this needs to be moved somewhere soon
1818
}
1919

20-
private void Button_Click(object sender, RoutedEventArgs e)
21-
{
22-
AuthenticationWindow authenticationWindow = new AuthenticationWindow();
23-
authenticationWindow.Show();
24-
}
25-
26-
/// <summary>
27-
/// Occurs when a navigation item is clicked to navigate to its page
28-
/// </summary>
29-
/// <param name="sender">The source of the event.</param>
30-
/// <param name="e">The event data.</param>
31-
private void MainWindowNavigationItem_Click(object sender, RoutedEventArgs e)
32-
{
33-
MainWindowNavigationItem mwniSender = (MainWindowNavigationItem)sender;
34-
tbContentTitle.Text = (string)mwniSender.Content;
35-
frContent.Navigate(mwniSender.Page);
36-
}
3720

3821
/// <summary>
3922
/// Occurs when the back/forward buttons are clicked
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.Windows.Input;
3+
4+
namespace ViewModel.Commands
5+
{
6+
/// <summary>
7+
/// Handles the click events of the main menu buttons
8+
/// </summary>
9+
public class NavigateButtonCommand : ICommand
10+
{
11+
/// <summary>
12+
/// The viewmodel modified by this command
13+
/// </summary>
14+
public MainPageViewModel VM { get; set; }
15+
16+
/// <summary>
17+
/// Creates a new NavigateButtonCommand
18+
/// </summary>
19+
/// <param name="vm">The viewmodel modified by this command</param>
20+
public NavigateButtonCommand(MainPageViewModel vm)
21+
{
22+
VM = vm;
23+
}
24+
25+
/// <summary>
26+
/// Defines the method that determines whether the command can execute in its current state.
27+
/// </summary>
28+
/// <param name="parameter">This value can be null, because it is not used.</param>
29+
/// <returns>true if this command can be executed; otherwise, false.</returns>
30+
public bool CanExecute(object parameter) => VM.MainNavigationItems.Count > 0;
31+
32+
/// <summary>
33+
/// Updates the viewmodel with the given information.
34+
/// </summary>
35+
/// <param name="parameter">The String or MainMenuNavigationItemData containing information about information where to navigate to</param>
36+
public void Execute(object parameter)
37+
{
38+
if (parameter.GetType() == typeof(string))
39+
{
40+
VM.CurrentVisiblePage = (string)parameter;
41+
VM.Title = "StudentMatcher";
42+
}
43+
else if (parameter.GetType() == typeof(MainPageViewModel.MainMenuNavigationItemData))
44+
{
45+
var data = (MainPageViewModel.MainMenuNavigationItemData)parameter;
46+
VM.CurrentVisiblePage = data.Page;
47+
VM.Title = data.Title;
48+
}
49+
else
50+
{
51+
throw new InvalidOperationException("Accepts only strings");
52+
}
53+
}
54+
55+
/// <summary>
56+
/// Occurs when changes occur that affect whether or not the command should execute.
57+
/// </summary>
58+
public event EventHandler CanExecuteChanged;
59+
}
60+
}

ViewModel/MainPageViewModel.cs

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
4+
using System.ComponentModel;
5+
using System.Text;
6+
using ViewModel.Commands;
7+
//using System.Windows.Controls;
8+
9+
namespace ViewModel
10+
{
11+
/// <summary>
12+
/// ViewModel for MainPage
13+
/// </summary>
14+
public class MainPageViewModel : INotifyPropertyChanged
15+
{
16+
#region Properties
17+
/// <summary>
18+
/// Collection of pages for the main navigation menu
19+
/// </summary>
20+
public ObservableCollection<MainMenuNavigationItemData> MainNavigationItems { get; } = new ObservableCollection<MainMenuNavigationItemData>()
21+
{
22+
new MainMenuNavigationItemData("Profile", "ProfilePage.xaml", null)
23+
};
24+
25+
26+
private string currentVisiblePage;
27+
/// <summary>
28+
/// Page that is currently visible on the frame
29+
/// </summary>
30+
public string CurrentVisiblePage
31+
{
32+
get
33+
{
34+
return currentVisiblePage;
35+
}
36+
set
37+
{
38+
currentVisiblePage = value;
39+
OnPropertyChanged("CurrentVisiblePage");
40+
}
41+
}
42+
43+
private string title;
44+
/// <summary>
45+
/// Title shown on top of the page
46+
/// </summary>
47+
public string Title
48+
{
49+
get
50+
{
51+
return title;
52+
}
53+
set
54+
{
55+
title = value;
56+
OnPropertyChanged("Title");
57+
}
58+
}
59+
60+
/// <summary>
61+
/// Handles the click events of the main menu buttons
62+
/// </summary>
63+
public NavigateButtonCommand NavigateButtonCommand { get; set; }
64+
#endregion
65+
66+
#region constructor
67+
/// <summary>
68+
/// Creates a new viewmodel for MainPage
69+
/// </summary>
70+
public MainPageViewModel()
71+
{
72+
NavigateButtonCommand = new NavigateButtonCommand(this);
73+
}
74+
#endregion
75+
76+
#region MainMenuNavigationItemData
77+
/// <summary>
78+
/// The data for a menu item on the main navigation menu
79+
/// </summary>
80+
public struct MainMenuNavigationItemData
81+
{
82+
/// <summary>
83+
/// Creates an item of data for the main navigation menu
84+
/// </summary>
85+
/// <param name="title">The title of the page shown on the top of the main page</param>
86+
/// <param name="page">The page to be navigated to</param>
87+
/// <param name="extraInformation">Optional extra information to be given when navigating</param>
88+
public MainMenuNavigationItemData(string title, string page, object extraInformation)
89+
{
90+
Title = title;
91+
Page = page;
92+
ExtraInformation = extraInformation;
93+
}
94+
95+
/// <summary>
96+
/// The title of the page shown on the top of the main page
97+
/// </summary>
98+
public string Title { get; set; }
99+
/// <summary>
100+
/// The page to be navigated to
101+
/// </summary>
102+
public string Page { get; set; }
103+
/// <summary>
104+
/// Optional extra information to be given when navigating
105+
/// </summary>
106+
public object ExtraInformation { get; set; }
107+
}
108+
#endregion
109+
110+
#region Property change notification
111+
/// <summary>
112+
/// Occurs when a property value changes.
113+
/// </summary>
114+
public event PropertyChangedEventHandler PropertyChanged;
115+
/// <summary>
116+
/// Triggers the PropertyChanged event
117+
/// </summary>
118+
/// <param name="propertyName">The property which is changed</param>
119+
private void OnPropertyChanged(string propertyName)
120+
{
121+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
122+
}
123+
#endregion
124+
}
125+
}

0 commit comments

Comments
 (0)