Click here to Skip to main content
15,885,278 members
Articles / Desktop Programming / WPF

Porting from WPF to Silverlight: The Missing Pieces, Part 1

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
21 Sep 2010Ms-PL1 min read 18.6K   3   2
Porting from WPF to Silverlight: The Missing Pieces, Part 1

Recently I’ve been working on a port of a WPF 3.5 application into a Silverlight 3 web application. I came across many of the limitations of Silverlight 3 relative to WPF 3.5.

Fortunately, I’ve found numerous excellent solutions throughout the blogosphere. These have helped me so much I would like to note them for all to see. Also, this list might come in handy to other people porting applications from WPF to Silverlight.

One final note, the port was done from WPF 3.5 to Silverlight 3. Some of the issues might have been solved in Silverlight 4 or have different solutions.

No Commands

Description: Silverlight 3 lacks proper support for commands.

Although Silverlight 3 does have the definition of ICommand, the buttons don’t have a Command property to bind with.

Solution by: Patrick Cauldwell

http://www.cauldwell.net/patrick/blog/MVVMBindingToCommandsInSilverlight.aspx

In a nutshell:

Instead of writing:

XML
<Button 
    Content="Say Hello" 
    Command="{Binding Path=SayHello}"
    />

Use the code in the post and write:

XML
<Button 
    Content="Say Hello" 
    my:ButtonService.Command="{Binding Path=SayHello}"
    />

No ClipToBounds

Description: Silverlight 3 lacks support for ClipToBounds.

Solution by: Colin Eberhardt

http://www.scottlogic.co.uk/blog/colin/2009/05/silverlight-cliptobounds-can-i-clip-it-yes-you-can/

In a nutshell:

Instead of writing:

XML
<Grid 
    Background="Yellow" 
    Margin="20,40,-20,20" 
    ClipToBounds="True"
    >
    ...
</Grid>

Use the code in the post and write:

XML
<Grid 
    Background="Yellow" 
    Margin="20,40,-20,20" 
    util:Clip.ToBounds="True"
    >
    ...
</Grid>

No DataContextChanged Event

Description: Silverlight 3 doesn’t have the DataContextChanged event.

Solution by: Emiel Jongerius

http://www.pochet.net/blog/2010/06/16/silverlight-datacontext-changed-event-and-trigger/

In a nutshell:

Instead of writing:

C#
MyControl.DataContextChanged += MyControl_DataContextChanged;

Use the code in the post and write:

C#
DataContextChangedHelper.AddDataContextChangedHandler(MyControl, 
    MyControl_DataContextChanged);

No DataTrigger

Description: Silverlight 3 doesn’t have data triggers, it uses a completely different model, using VisualStateManager.

Solution by: Pete Blois

http://blois.us/blog/2009/04/datatrigger-bindings-on-non.html

In a nutshell:

Instead of writing:

XML
<DataTrigger Binding="{Binding IsLoaded}" Value="True">    ... DOING STUFF<DataTrigger>

Use the code in the post and write:

XML
<i:Interaction.Behaviors>
    <id:DataStateSwitchBehavior Binding='{Binding IsLoaded}'>
        <id:DataStateSwitchCase Value='True' State='Loaded'/>
    </id:DataStateSwitchBehavior>
</i:Interaction.Behaviors>

Of course, you should define a state that does the same “STUFF”.

The transition is not immediate, but at least it provides the same functionality as data triggers.

To Be Continued...

That’s it for now,
Arik Poznanski.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior) Verint
Israel Israel
Arik Poznanski is a senior software developer at Verint. He completed two B.Sc. degrees in Mathematics & Computer Science, summa cum laude, from the Technion in Israel.

Arik has extensive knowledge and experience in many Microsoft technologies, including .NET with C#, WPF, Silverlight, WinForms, Interop, COM/ATL programming, C++ Win32 programming and reverse engineering (assembly, IL).

Comments and Discussions

 
GeneralMy vote of 5 Pin
Julien Bouvier6-Oct-10 15:47
Julien Bouvier6-Oct-10 15:47 
GeneralRe: My vote of 5 Pin
Arik Poznanski11-Oct-10 4:03
Arik Poznanski11-Oct-10 4:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.