Click here to Skip to main content
15,885,767 members
Articles / Desktop Programming / WPF

A Practical Quick-start Tutorial on MVVM in WPF

Rate me:
Please Sign up or sign in to vote.
4.76/5 (72 votes)
21 May 2010CPOL11 min read 480.8K   18.4K   257  
This article gives a practical quick-start tutorial on MVVM in WPF for application developers.
<Window x:Class="WpfModelViewDemoApplication.Views.MainView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:c="clr-namespace:WpfModelViewDemoApplication.Commands"
    FontFamily="Verdana"
    Title="WPF MVVM Tutorial">
    
<Window.Resources>
    <c:CommandReference x:Key="ExitCommandReference" Command="{Binding ExitCommand}" />
        <Style x:Key="LabelStyle" TargetType="{x:Type TextBlock}">
            <Setter Property="FontWeight" Value="Bold" />
            
        </Style>
        <Style x:Key="GridViewHeaderStyle" TargetType="{x:Type GridViewColumnHeader}">
            <Setter Property="FontWeight" Value="Bold" />
            <Setter Property="Foreground" Value="Maroon" />
            <Setter Property="Background" Value="LightSkyBlue" />
        </Style>
    </Window.Resources>
   
<Window.InputBindings>
    <KeyBinding Key="X" Modifiers="Control" Command="{StaticResource ExitCommandReference}" /> 
</Window.InputBindings>

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

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

    <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}">
            </TextBox>
            <TextBlock Style="{StaticResource  LabelStyle}">Score</TextBlock>
                <TextBox Width="100" Margin="10, 0, 5, 0"
                         Text="{Binding Path=StudentScoreToAdd, Mode=OneWayToSource}">
                </TextBox>
            
                    <Button x:Name="btnAddStudent" Content="Add a student" Command="{Binding AddStudent}">
            </Button>
        </StackPanel>
    </Grid>

        <ListView  Grid.Row="2" BorderBrush="White" ItemsSource="{Binding Path=Students}"
                   HorizontalAlignment="Stretch">
            <ListView.View>
                <GridView>
                    <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}" />
                </GridView>
            </ListView.View>
        </ListView >
    </Grid>
</Window>

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.

License

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


Written By
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.

Comments and Discussions