MVVM RI of Prism, How to assign ViewModel for main Page?

Jan 9, 2012 at 2:11 PM

Hi,

MVVM RI of Prism, How to assign ViewModel for main Page? I know how to set for other page but can not do that with Main page.

More detail follow this http://msdn.microsoft.com/en-us/library/gg405492(v=PandP.40).aspx

Thanks And Best Regards.

Jan 9, 2012 at 3:55 PM
Edited Jan 9, 2012 at 3:57 PM

Hi

I'm not sure I completely understand your question... but, to add a viewModel to the MainPage.xaml you can do the following.

If you want to use a new ViewModel, then you should create a new class in the ViewModels folder, call it whatever you want ie: MyViewModel.cs

 

using Microsoft.Practices.Prism.ViewModel;

namespace MVVM.ViewModels
{
    public class MyViewModel : NotificationObject
    {
        public string Customtext { get; set; }

        public MyViewModel()
        {
            Customtext = "Hello World";
        }
    }
}

 

 

then all you need to do is hook up the main page to the ViewModel, add the following to your MainPage.xaml

 

    <UserControl.DataContext>
        <basicvm:MyViewModel />
    </UserControl.DataContext>
    

 

just make sure you are still importing the namespace and they are called the same... xmlns:basicvm="clr-namespace:MVVM.ViewModels" and <basicvm:MyViewModel />.
Then you can bind to the viewModel as per usual, you can add the following to the stackpanel in to test it.

 

<TextBlock HorizontalAlignment="Left" Width="Auto" Text="{Binding Path=Customtext}" FontWeight="Normal" Foreground="#FFFFFFFF" FontSize="24" FontFamily="Comic Sans MS" VerticalAlignment="Center"/>

So, after you have finished your mainPage.xaml should look something like this

<UserControl x:Class="MVVM.MainPage"
             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:basicv="clr-namespace:MVVM.Views"
             xmlns:basicvm="clr-namespace:MVVM.ViewModels"
             mc:Ignorable="d"
             d:DesignHeight="600" d:DesignWidth="600">

    <UserControl.DataContext>
        <basicvm:vm />
    </UserControl.DataContext>
    
    <Grid Background="{StaticResource MainBackground}">
        <Grid MinWidth="300" MaxWidth="800">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <StackPanel Grid.Row="0" Orientation="Horizontal">
                <TextBlock HorizontalAlignment="Left" Width="Auto" Text="MVVM" FontWeight="Bold" Foreground="#FFFFFFFF" FontSize="24" FontFamily="Comic Sans MS" Margin="5"/>
                <TextBlock HorizontalAlignment="Left" Width="Auto" Text="QuickStart" FontWeight="Normal" Foreground="#FFFFFFFF" FontSize="24" FontFamily="Comic Sans MS" VerticalAlignment="Center"/>
                <TextBlock HorizontalAlignment="Left" Width="Auto" Text="{Binding Path=Customtext}" FontWeight="Normal" Foreground="#FFFFFFFF" FontSize="24" FontFamily="Comic Sans MS" VerticalAlignment="Center"/>
            </StackPanel>
            
            <basicv:QuestionnaireView Grid.Row="1" />
        </Grid>
    </Grid>
</UserControl>

Hope that helps.