Click here to Skip to main content
12,816,406 members (28,863 online)
Click here to Skip to main content


251 bookmarked
Posted 15 May 2010

A Practical Quick-start Tutorial on MVVM in WPF

, 21 May 2010 CPOL
This article gives a practical quick-start tutorial on MVVM in WPF for application developers.
<Window x:Class="WpfModelViewDemoApplication.Views.MainView"
    Title="WPF MVVM Tutorial">
    <c:CommandReference x:Key="ExitCommandReference" Command="{Binding ExitCommand}" />
        <Style x:Key="LabelStyle" TargetType="{x:Type TextBlock}">
            <Setter Property="FontWeight" Value="Bold" />
        <Style x:Key="GridViewHeaderStyle" TargetType="{x:Type GridViewColumnHeader}">
            <Setter Property="FontWeight" Value="Bold" />
            <Setter Property="Foreground" Value="Maroon" />
            <Setter Property="Background" Value="LightSkyBlue" />
    <KeyBinding Key="X" Modifiers="Control" Command="{StaticResource ExitCommandReference}" /> 

        <RowDefinition Height="Auto" />
        <RowDefinition Height="40" />
        <RowDefinition Height="*" />

    <Menu Grid.Row="0">
        <MenuItem Header="_File">
            <MenuItem Command="{Binding ExitCommand}" Header="E_xit" InputGestureText="Ctrl-X" />

    <Grid Grid.Row="1" HorizontalAlignment="Right" Margin="0, 5, 20, 10" VerticalAlignment="Center">
        <StackPanel Orientation="Horizontal">
            <TextBlock Style="{StaticResource  LabelStyle}">Student name</TextBlock>
            <TextBox Width="200" Margin="10, 0, 5, 0" Text="{Binding Path=StudentNameToAdd, Mode=OneWayToSource}">
            <TextBlock Style="{StaticResource  LabelStyle}">Score</TextBlock>
                <TextBox Width="100" Margin="10, 0, 5, 0"
                         Text="{Binding Path=StudentScoreToAdd, Mode=OneWayToSource}">
                    <Button x:Name="btnAddStudent" Content="Add a student" Command="{Binding AddStudent}">

        <ListView  Grid.Row="2" BorderBrush="White" ItemsSource="{Binding Path=Students}"
                    <GridViewColumn Header="Name" HeaderContainerStyle="{StaticResource GridViewHeaderStyle}"
                                    DisplayMemberBinding="{Binding Path=Name}" />
                    <GridViewColumn Header="Score" HeaderContainerStyle="{StaticResource GridViewHeaderStyle}"
                                    DisplayMemberBinding="{Binding Path=Score}" />
                    <GridViewColumn Header="TimeAdded" HeaderContainerStyle="{StaticResource GridViewHeaderStyle}"
                                    DisplayMemberBinding="{Binding Path=TimeAdded}" />
                    <GridViewColumn Header="Comment" HeaderContainerStyle="{StaticResource GridViewHeaderStyle}"
                                    DisplayMemberBinding="{Binding Path=Comment}" />
        </ListView >

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Dr. Song Li
United States United States
I have been working in the IT industry for some time. It is still exciting and I am still learning. I am a happy and honest person, and I want to be your friend.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 21 May 2010
Article Copyright 2010 by Dr. Song Li
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid