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

Presentation Model (MVVM) Good Practices

Rate me:
Please Sign up or sign in to vote.
4.81/5 (13 votes)
11 May 2010CPOL16 min read 68.2K   944   76  
Showing some good practices that can be applied to the Presentation Model/MVVM pattern.
<UserControl
    x:Class="WPFUI.View.ProductEditView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Style="{StaticResource ViewStyle}"
    >
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition Height="30"/>
            <RowDefinition Height="30"/>
            <RowDefinition Height="30"/>
            <RowDefinition Height="30"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <DockPanel
            Margin="5"
            Grid.Row="0"
            Grid.Column="0"
            >
            <TextBlock
                Width="50"
                DockPanel.Dock="Left"
                Text="Name : "
                />
            <TextBox
                Width="150"
                DockPanel.Dock="Right"
                Text="{Binding ProductName}"
                />
        </DockPanel>
        <DockPanel
            Margin="5"
            Grid.Row="1"
            Grid.Column="0"
            >
            <TextBlock                
                Width="50"
                DockPanel.Dock="Left"
                Text="Price : "
                />
            <TextBox
                Width="150"
                DockPanel.Dock="Right"
                Text="{Binding ProductPrice}"
                />
        </DockPanel>
        <DockPanel
            Margin="5"
            Grid.Row="0"
            Grid.Column="1"
            Grid.RowSpan="5">
            <TextBlock
                Width="50"
                Text="Photo : "
                DockPanel.Dock="Left"
                />
            <StackPanel
                DockPanel.Dock="Right">
                <Border
                    BorderThickness="1"
                    BorderBrush="Black">
                    <Image                        
                        Height="100"
                        Width="100"
                        Source="{Binding ProductPhotography}"
                        />
                </Border>
                <Button
                    Content="Change"
                    Command="{Binding Call}"
                    CommandParameter="ChoosePhoto"
                    />
            </StackPanel>
        </DockPanel>
        <Button 
            VerticalAlignment="Bottom"
            HorizontalAlignment="Right"
            Grid.Column="3"
            Grid.Row="6"
            Content="Save"
            Command="{Binding Call}"
            CommandParameter="SaveOrUpdate"
            />
    </Grid>
</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
Brazil Brazil
Software developer specialized in the .NET framework

Comments and Discussions