Click here to Skip to main content
15,885,278 members
Articles / Programming Languages / C#

Migrate from Basic to MVVM and MEF Composable Patterns for a Silverlight Application - Part 3

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
3 May 2012CPOL12 min read 24.7K   464   9  
The article series shows how to upgrade a Silverlight application having basic patterns to the MVVM and MEF composable patterns with easy approaches and detailed coding explanations.
<UserControl x:Class="ProductApp.Views.ProductList"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
             xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
             mc:Ignorable="d" d:DesignHeight="298" d:DesignWidth="418">
    <UserControl.Resources>
        <Style x:Key="AlignRight" TargetType="sdk:DataGridCell">
            <Setter Property="HorizontalContentAlignment" Value="Right" />
        </Style>
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot"
          Style="{StaticResource LayoutRootGridStyle}">
        <Grid Style="{StaticResource NavigationOuterGridStyle}">
            <ScrollViewer x:Name="PageScrollViewer" Style="{StaticResource PageScrollViewerStyle}">
                <StackPanel x:Name="ContentStackPanel" Style="{StaticResource ContentStackPanelStyle}">
                    <TextBlock Text="Product List" Style="{StaticResource HeaderTextStyle}" />
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                        <TextBlock Height="22" HorizontalAlignment="Left"
                                   Name="FilterLabel" Text="Category" />
                        <ComboBox Height="23" Margin="6"
                                  Name="categoryCombo" Width="150"
                                  ItemsSource="{Binding Path=CategoryItems}"
                                  DisplayMemberPath="CategoryName"
                                  SelectedValuePath="CategoryID"
                                  SelectedItem="{Binding Path=SelectedCategory, Mode=TwoWay}">
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="SelectionChanged">
                                    <i:InvokeCommandAction Command="{Binding CategorySelectionChanged}" />
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                        </ComboBox>
                    </StackPanel>
                    <sdk:DataGrid AutoGenerateColumns="False"
                                  Name="productDataGrid" Margin="6,10,0,0" MinHeight="100"
                                  Height="Auto" Width="400"
                                  HorizontalAlignment="Left"
                                  ItemsSource="{Binding Path=ProductItems}"
                                  SelectedItem="{Binding Path=CurrentProduct, Mode=TwoWay}">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="SelectionChanged">
                                <i:InvokeCommandAction Command="{Binding DataGridSelectionChanged}" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                        <sdk:DataGrid.Columns>
                            <sdk:DataGridTextColumn Header="Product ID"
                                                    Binding="{Binding ProductID}" />
                            <sdk:DataGridTextColumn Header="Product Name"
                                                    Binding="{Binding ProductName}" />
                            <sdk:DataGridTextColumn Header="Unit Price"
                                                    Binding="{Binding UnitPrice}"
                                                    CellStyle="{StaticResource AlignRight}" />
                            <sdk:DataGridCheckBoxColumn Header="Out of Stock" Width="*"
                                                        Binding="{Binding OutOfStock}" />
                        </sdk:DataGrid.Columns>
                    </sdk:DataGrid>
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Left"
                                Height="51" Width="288">
                        <Button Content="Add" Height="23"
                                Name="addButton" Width="75" Margin="6"
                                Command="{Binding Path=AddProductCommand}"
                                CommandParameter="AddProductWindowView" />
                        <Button Content="Save"  Height="23"
                                Name="saveButton" Width="75" Margin="10"
                                Command="{Binding Path=SaveChangesCommand}" />
                        <Button Content="Delete" Height="23"
                                Name="deleteButton" Width="75" Margin="10"
                                Command="{Binding Path=DeleteProductCommand}" />
                    </StackPanel>
                </StackPanel>
            </ScrollViewer>
        </Grid>
    </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
United States United States
Shenwei is a software developer and architect, and has been working on business applications using Microsoft and Oracle technologies since 1996. He obtained Microsoft Certified Systems Engineer (MCSE) in 1998 and Microsoft Certified Solution Developer (MCSD) in 1999. He has experience in ASP.NET, C#, Visual Basic, Windows and Web Services, Silverlight, WPF, JavaScript/AJAX, HTML, SQL Server, and Oracle.

Comments and Discussions