Click here to Skip to main content
15,896,557 members
Articles / Desktop Programming / XAML

A Pluggable Architecture for Building Silverlight Applications with MVVM

Rate me:
Please Sign up or sign in to vote.
4.71/5 (23 votes)
6 Jul 2011CPOL7 min read 145.8K   2.2K   90  
This article describes building a sample Silverlight application with the MVVM Light toolkit, WCF RIA Services, and a pluggable application architecture using MEF.
<UserControl x:Class="IssueVision.Client.Reports"
    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:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    xmlns:common="clr-namespace:IssueVision.Common;assembly=IssueVision.Common"
    Loaded="UserControl_Loaded"  Unloaded="UserControl_Unloaded"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
    <UserControl.Resources>
        <common:IssueBrushConverter x:Key="issueBrushConverter"/>
    </UserControl.Resources>
    
    <Grid x:Name="LayoutRoot">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="2*" />
            <RowDefinition Height="3*" />
        </Grid.RowDefinitions>
        <Grid Grid.Row="0" Grid.Column="0" x:Name="grid_Charts">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <chartingToolkit:Chart Title="Bug Trends by Month">
                <chartingToolkit:LineSeries Title="Active Bugs"
                                        ItemsSource="{Binding Path=ActiveBugCountByMonth}"
                                        IndependentValueBinding="{Binding Path=Month}"
                                        DependentValueBinding="{Binding Path=Count}" />
                <chartingToolkit:LineSeries Title="Resolved Bugs"
                                        ItemsSource="{Binding Path=ResolvedBugCountByMonth}"
                                        IndependentValueBinding="{Binding Path=Month}"
                                        DependentValueBinding="{Binding Path=Count}" />
            </chartingToolkit:Chart>
            <chartingToolkit:Chart Grid.Column="1" Title="Bug Counts by Priority">
                <chartingToolkit:ColumnSeries Title="Bug Counts"
                                          ItemsSource="{Binding Path=ActiveBugCountByPriority}"
                                          IndependentValueBinding="{Binding Path=Priority}"
                                          DependentValueBinding="{Binding Path=Count}" />
            </chartingToolkit:Chart>
        </Grid>
        <ListBox Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"
                 ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                 ItemsSource="{Binding Path=AllIssues}" >
                <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem">
                        <Setter Property="HorizontalAlignment" Value="Stretch" />
                        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                    </Style>
                </ListBox.ItemContainerStyle>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Border BorderBrush="Silver" BorderThickness="2"
                                Padding="4" HorizontalAlignment="Stretch"
                                Background="{Binding Converter={StaticResource issueBrushConverter}, 
                                             ValidatesOnNotifyDataErrors=False}" >
                            <StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="{Binding Path=IssueID, StringFormat='Issue Id: \{0\} ',
                                                      ValidatesOnNotifyDataErrors=False}" />
                                    <TextBlock Text="{Binding Path=Title, ValidatesOnNotifyDataErrors=False}" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="{Binding Path=Priority, StringFormat='Pri: \{0\} ',
                                                      ValidatesOnNotifyDataErrors=False}" />
                                    <TextBlock Text="{Binding Path=Severity, StringFormat='Sev: \{0\}',
                                                      ValidatesOnNotifyDataErrors=False}" />
                                </StackPanel>
                                <TextBlock TextTrimming="WordEllipsis" TextWrapping="Wrap" MaxHeight="40"
                                           Text="{Binding Path=Description, StringFormat='Description: \{0\}',
                                                  TargetNullValue='No Description', ValidatesOnNotifyDataErrors=False}" />
                            </StackPanel>
                        </Border>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        <Button Grid.Row="0" Grid.Column="1"
                VerticalAlignment="Top" HorizontalAlignment="Right" 
                Width="75" Height="23" Margin="4,4,4,0"
                Content="Print Report"
                Command="{Binding Path=PrintReportCommand}"/>
        </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
Software Developer (Senior)
United States United States
Weidong has been an information system professional since 1990. He has a Master's degree in Computer Science, and is currently a MCSD .NET

Comments and Discussions