The first, most fully featured and obvious controls for navigation are Frame and Page. You could include a Frame in your main window and run it all from that, but if your project is going to be completely based around Page navigation, you can simply use a NavigationWindow (from System.Windows.Navigation) instead of a Window for your main startup interface. MainWindow.xaml
<
NavigationWindow
x:Class
=
"ApplicationNavigation.MainWindow"
xmlns
"http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
"http://schemas.microsoft.com/winfx/2006/xaml"
Title
"Navigation Window"
Height
"600"
Width
"550"
Background
"#FFFFE9E9"
Source
"View/Page0.xaml"
>
</
This first navigation is triggered by the Source property of the NavigationWindow.
This is the most simple method for navigating between pages within a NavigationWindow or frame. There is no code-behind needed. It is therefore quite similar to a web page hyperlink. Page0.xaml
TextBlock
Grid.Row
"1"
HorizontalAlignment
"Center"
FontWeight
"Bold"
FontSize
"18"
Margin
"0,20,0,0"
Hyperlink
NavigateUri
"Page1.xaml"
>1. Click this hyperlink</
Grid
Grid.RowDefinitions
RowDefinition
"Auto"
/>
Text
"Page 1"
Button
Click
"Button_Click"
Content
"2. Frame Navigate"
"200"
Frame
x:Name
"MyFrame"
"2"
BorderThickness
"10,10,10,101"
BorderBrush
"#FFB4B4B4"
"Frame"
Padding
"10"
private
void
Button_Click(
object
sender, RoutedEventArgs e)
{
MyFrame.Navigate(
new
Page2());
}
this
.NavigationService.Navigate(
Page3());
Page
"ApplicationNavigation.View.Page3"
"#FFEBFFD5"
"Page3"
xmlns:views
"clr-namespace:ApplicationNavigation.View"
"Page 3"
Border
"MyStage"
CornerRadius
"Black"
views:UserControl1
var parent = Parent
as
Border;
if
(parent !=
null
)
parent.Child =
UserControl2(parent);
using
System.Windows;
System.Windows.Controls;
namespace
ApplicationNavigation.View
public
partial
class
UserControl2 : UserControl
Border _parentBorder;
UserControl2(Border parentBorder)
_parentBorder = parentBorder;
InitializeComponent();
(_parentBorder !=
_parentBorder.Child =
UserControl3();
Page3()
ApplicationController.RootBorder = MyStage;
...
ApplicationNavigation.Helpers
ApplicationController
static
Border RootBorder {
get
;
set
; }
LoadUserControl(UserControl nextControl)
RootBorder.Child = nextControl;
ApplicationController.LoadUserControl(newControl);
var newControl =
UserControl4();
newControl.NavigateEvent +=
EventHandler(newControl_NavigateEvent);
newControl_NavigateEvent(
sender, EventArgs e)
ApplicationController.LoadUserControl(
UserControl5());
event
EventHandler NavigateEvent;
UserControl4()
(NavigateEvent !=
) NavigateEvent(
,
);
IDictionary<
string
, List<Action<
>>> pl_dict =
Dictionary<
>>>();
Register(
token, Action<
> callback)
(!pl_dict.ContainsKey(token))
var list =
List<Action<
>>();
list.Add(callback);
pl_dict.Add(token, list);
else
bool
found =
false
foreach
(var item
in
pl_dict[token])
(item.Method.ToString() == callback.Method.ToString())
true
(!found)
pl_dict[token].Add(callback);
Mediator.Register(
"NavigateMessage"
, GoNextUserControl);
GoNextUserControl(
param)
MyStage.Child = param
UserControl6;
namespace ApplicationNavigation.View
public partial class UserControl6 : UserControl, INotifyPropertyChanged
UIElement _CurrentPage;
public UIElement CurrentPage
return _CurrentPage;
if (_CurrentPage != value)
_CurrentPage = value;
RaisePropertyChanged("CurrentPage");
public UserControl6()
DataContext = this;
void RaisePropertyChanged(string prop)
if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); }
public event PropertyChangedEventHandler PropertyChanged;
ContentPresenter
"{Binding CurrentPage}"
Grid.RowSpan
CurrentPage =
UserControl7();
"22"
"UserControl 7"
StackPanel
Orientation
"Horizontal"
"10. MVVM - Please select:"
"0,0,10,0"
VerticalAlignment
ComboBox
ItemsSource
"{StaticResource People}"
DisplayMemberPath
"FirstName"
SelectedItem
"{Binding SelectedPerson, Mode=TwoWay}"
GroupBox
Header
"User Management"
"{Binding ManagementControl}"
UIElement ManagementControl
return
(UIElement)GetValue(ManagementControlProperty); }
{ SetValue(ManagementControlProperty, value); }
readonly
DependencyProperty ManagementControlProperty =
DependencyProperty.Register(
"ManagementControl"
typeof
(UIElement),
(UserControl7),
UIPropertyMetadata(
));
Person SelectedPerson
(Person)GetValue(SelectedPersonProperty); }
{ SetValue(SelectedPersonProperty, value); }
DependencyProperty SelectedPersonProperty =
"SelectedPerson"
(Person),
, SelectedPersonChanged));
SelectedPersonChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
var person = e.NewValue
Person;
(person !=
var view = ApplicationController.MakePersonAdminControl(person.Id);
(obj
UserControl7).ManagementControl = view;
UIElement MakePersonAdminControl(
int
id)
var view =
PersonAdminControl();
// Note: No parameters, just a dumb skin
view.DataContext =
PersonViewModel(id);
view;
x:Array
x:Key
"People"
Type
"model:Person"
xmlns:sys
"clr-namespace:System;assembly=mscorlib"
model:Person
model:Person.Id
>1</
model:Person.FirstName
>Peter</
model:Person.LastName
>Laker</
model:Person.Age
>42</
model:Person.DepartmentId
model:Person.HappyRating
>90</
model:Person.StartDate
>12/12/12 0:0:0</
>2</
>Joe</
>Bloggs</
>30</
>3</
>50</
>11/11/11</
>Jane</
>Doe</
>24</
>0</
model:Person.IsDead
>True</
ObservableCollection<Person> Persons {