Click here to Skip to main content
15,891,184 members
Articles / Desktop Programming / WPF

WPF: MVVM (Model View View-Model) Simplified

Rate me:
Please Sign up or sign in to vote.
4.69/5 (87 votes)
20 May 2009CPOL4 min read 438.5K   13.6K   186  
Provides a clear and simple sample that clearly illustrates MVVM and its usage
<UserControl x:Class="OliverCode.MVVM.View.PersonView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="Auto" Width="Auto"
    xmlns:local="clr-namespace:OliverCode.MVVM.ViewModel">
    <StackPanel Orientation="Vertical" Margin="4">
        <!--Here is where we the view gets a copy to the ViewModel Declaratively-->
        <StackPanel.DataContext>
            <local:PersonViewModel />
        </StackPanel.DataContext>        
        <StackPanel Orientation="Vertical" DataContext="{Binding Path=Person, Mode=TwoWay}" Margin="4">
            <StackPanel Orientation="Horizontal">
                <Label Content="First Name:" Margin="0,0,4,0"/>
                <TextBox Width="250" Text="{Binding Path=FirstName}"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal" Margin="0,5,0,0">
                <Label Content="Last Name:" Margin="0,0,4,0"/>
                <TextBox Width="250" Text="{Binding Path=LastName}"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal" Margin="0,5,0,0">
                <Label Content="Age:" Margin="35,0,4,0"/>
                <TextBox Width="50" MaxLength="3" Text="{Binding Path=Age}"/>
            </StackPanel>
        </StackPanel>        
        <StackPanel>
            <Button Content="Save" HorizontalAlignment="Right" Width="80" Command="{Binding Path=SavePersonCommand}"/>
        </StackPanel>
    </StackPanel>
</UserControl>

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
Software Developer (Senior) 4CoreDev
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions