C# MVVM Tutorial 2 – This method did NOT work!

I tried this the first time and it DID NOT WORK!! Ahhhh…   Go back to the working Tutorial 2 here.

 

●  Right click the Page3 folder in the Content folder of the project solution and copy.  Right click on the Content folder and paste.  Rename the files to include Page4:

●  Update Code by changing all references of Page3 to Page4.  I started with the Page4ViewModel.cs file.  REFACTOR Code to change the names correctly or you will have issues!  See updated Page4ViewModel.cs below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using GalaSoft.MvvmLight;
 
namespace MvvmControlChange.Content.Page4.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class Page4ViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the Page3ViewModel class.
        /// </summary>
        public Page4ViewModel()
        {
        }
        public string InfoTitlePage4
        {
            get
            {
                return "MVVM LIGHT p4";
            }
        }
 
        public string PageTitle
        {
            get
            {
                return "Page 4";
            }
        }
    }
}
using GalaSoft.MvvmLight;

namespace MvvmControlChange.Content.Page4.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class Page4ViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the Page3ViewModel class.
        /// </summary>
        public Page4ViewModel()
        {
        }
        public string InfoTitlePage4
        {
            get
            {
                return "MVVM LIGHT p4";
            }
        }

        public string PageTitle
        {
            get
            {
                return "Page 4";
            }
        }
    }
}

●  Update the Page4View.xaml file to include references to Page4:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<UserControl x:Class="MvvmControlChange.Content.Page4.View.Page4View"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:ignore="http://www.ignore.com"
             mc:Ignorable="d ignore"
             DataContext="{Binding Page4ViewModel, Source={StaticResource Locator}}"
             d:DesignHeight="169"
             d:DesignWidth="255">
 
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="43*" />
            <RowDefinition Height="65*" />
        </Grid.RowDefinitions>
 
        <StackPanel x:Name="TitlePanel"
                    Grid.Row="0"
                    Margin="24,24,0,12">
            <TextBlock x:Name="InfoTitlePage4"
                       Text="{Binding InfoTitlePage4}" />
            <TextBlock x:Name="PageTitle"
                       Text="{Binding PageTitle}"
                       Margin="-3,10,0,0" />
        </StackPanel>
    </Grid>
</UserControl>
<UserControl x:Class="MvvmControlChange.Content.Page4.View.Page4View"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:ignore="http://www.ignore.com"
             mc:Ignorable="d ignore"
             DataContext="{Binding Page4ViewModel, Source={StaticResource Locator}}"
             d:DesignHeight="169"
             d:DesignWidth="255">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="43*" />
            <RowDefinition Height="65*" />
        </Grid.RowDefinitions>

        <StackPanel x:Name="TitlePanel"
                    Grid.Row="0"
                    Margin="24,24,0,12">
            <TextBlock x:Name="InfoTitlePage4"
                       Text="{Binding InfoTitlePage4}" />
            <TextBlock x:Name="PageTitle"
                       Text="{Binding PageTitle}"
                       Margin="-3,10,0,0" />
        </StackPanel>
    </Grid>
</UserControl>

 

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" extra="">