Click here to Skip to main content
15,884,472 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 67.8K   944   76  
Showing some good practices that can be applied to the Presentation Model/MVVM pattern.
<UserControl 
    x:Class="WPFUI.View.ProductsView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:tk="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
    xmlns:acb="clr-namespace:AttachedCommandBehavior;assembly=AttachedCommandBehavior"
    Style="{StaticResource ViewStyle}">
    <Grid>        
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition/>
            <RowDefinition Height="30"/>
        </Grid.RowDefinitions>
        <Button 
            Grid.Row="0"
            HorizontalAlignment="Left"
            Content="Load Data"
            Command="{Binding Call}"
            CommandParameter="LoadData"
            />
        <ListView
            Grid.Row="1"
            Style="{StaticResource EntityCollectionViewListStyle}"
            ItemsSource="{Binding EntityCollection}"  
            >
            <ListView.View>
                <GridView>
                    <GridViewColumn
                        Header="Name"
                        DisplayMemberBinding="{Binding ProductName}"
                        Width="120"/>
                    <GridViewColumn
                        Header="Price"
                        DisplayMemberBinding="{Binding ProductPrice}"
                        Width="70"/>
                    <GridViewColumn
                        Header="Photo"
                        Width="50">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Image 
                                    Source="{Binding ProductPhotography}" 
                                    Width="30" 
                                    Height="35"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>            
        </ListView>
        <Button 
            Grid.Row="2"
            HorizontalAlignment="Left"
            Content="Remove Selected"
            Command="{Binding Call}"
            CommandParameter="RemoveSelected"
            IsEnabled="{Binding CanRemove}"
            />
    </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