Click here to Skip to main content
15,886,422 members
Articles / Desktop Programming / WPF

Building an Extensible Application with MEF, WPF, and MVVM

Rate me:
Please Sign up or sign in to vote.
4.88/5 (45 votes)
15 Nov 2009LGPL316 min read 302K   7.4K   185  
An article for anyone interested in how to build an extensible application using WPF and the Model-View-ViewModel pattern.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:SoapBox.Demo.PinBall"
    xmlns:arena="clr-namespace:SoapBox.Core.Arena;assembly=SoapBox.Core.Arena"
    x:Class="SoapBox.Demo.PinBall.PinBallView">

    <!-- Make it a shiny ball -->
    <DataTemplate DataType="{x:Type local:PinBallSprite}">
        <Path Stroke="Black" StrokeThickness="1" Data="{Binding Path=Geometry}">
            <Path.Fill>
                <RadialGradientBrush GradientOrigin="0.33,0.33">
                    <GradientStop Offset="0" Color="White"/>
                    <GradientStop Offset="1" Color="Black"/>
                </RadialGradientBrush>
            </Path.Fill>
        </Path>
    </DataTemplate>

    <!-- Orient the ball within the arena (doesn't rotate because it's a sphere and 
            we want the shiny spot in the same spot all the time) -->
    <DataTemplate DataType="{x:Type local:PinBall}">
        <ContentControl Content="{Binding Path=(arena:IArenaBody.Sprite)}">
            <ContentControl.RenderTransform>
                <TransformGroup>
                    <!-- scale it, including inverting the Y axis -->
                    <ScaleTransform ScaleX="{Binding State.Scale}" ScaleY="{Binding State.Scale}"/>
                    <!-- offset it by it's physical position -->
                    <TranslateTransform X="{Binding State.ScreenX}" Y="{Binding State.ScreenY}" />
                </TransformGroup>
            </ContentControl.RenderTransform>
        </ContentControl>
    </DataTemplate>
</ResourceDictionary>

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 GNU Lesser General Public License (LGPLv3)


Written By
Engineer
Canada Canada
By day I'm a Professional Engineer, doing .NET, VB6, SQL Server, and Automation (Ladder Logic, etc.) programming.

On weekends I write and maintain an open source extensible application framework called SoapBox Core.

In the evenings I provide front line technical support for moms4mom.com and I help out with administrative tasks (like formatting stuff). I also pitch in as a moderator from time to time.

You can follow me on twitter.

Comments and Discussions