Pro Business Applications with Silverlight 4

Chris Anderson

Mentioned 5

Silverlight 4 has the potential to revolutionize the way we build business applications. With its flexibility, web deployment, cross-platform capabilities, rich .NET language support on the client, rich user interface control set, small runtime, and more, it comes close to the perfect platform in which to build business applications. It’s a very powerful technology, and despite its youth, it’s moving forward at a rapid pace and is gaining widespread adoption. This book will guide you through the process of designing and developing enterprise-strength business applications in Silverlight 4 and C#. You will learn how to take advantage of the power of Silverlight to develop rich and robust business applications, from getting started to deployment, and everything in between. In particular, this book will serve developers who want to learn how to design business applications, and introduce the patterns to use, the issues that you’ll face, and how to resolve them. Chris Anderson, who has been building line-of-business applications for years, demonstrates his experience through a candid presentation of how to tackle real-life issues, rather than just avoid them. Developers will benefit from his hard-won expertise through business application design patterns that he shares throughout the book. With this book in hand, you will Create a fully functional business application in Silverlight Discover how to satisfy all of the general requirements that most business applications need Develop a business application framework

More on

Mentioned in questions and answers.

I have read about dependency properties a lot on SO and other sites. But, really haven't found a great explanation and am still confused. I use both SL and WPF. Are they different in SL and WPF, in terms of implementation? Why do we really need them? Are they static means that their values are shared? Why was the reason MS introduced dependency properties?

Bounty: I am looking for a more thorough, complete answer.

Most of the questions you posed have been answered by others, but I'll give you my simple take on them.

First, they are different between Silverlight and WPF, with the WPF dependency properties having more capabilities. However, excluding these additional capabilities in WPF, they are fundamentally the same.

The big question is "when" should you use them (the "why" has mostly already been answered). You essentially need them when you need to assign a markup extension to them in XAML (such as a binding expression), which will then be resolved/evaluated at run time. For most of the time, this will only be necessary when writing custom controls that expose properties.

i.e. You drop a custom control on a surface in XAML, and assign a binding expression to one of its properties.

I see some people using them everywhere, thinking that they are required when doing Silverlight/WPF development, but this isn't the case. Different people have different lines that they draw in the sand, but I say only use them when necessary - which almost 100% of the time is within custom controls.

Blantant self promotion: I have deeper (Silverlight specific) discussion on this topic in chapter 10 of my book Pro Business Applications in Silverlight 4.

I have a page with two controls on it, a datagrid and a dataform.

In the datagrid, I have a list of all the objects of a certain class. When a user selects an item in the datagrid, the dataform is loaded with the selected object.

dataForm.CurrentItem = view.CurrentItem; view is a PagedCollectionView which contains only the selected item.

My problem is, when setting the dataform's currenitem property, if I use just the PagedCollectionView (view) without .CurrentItem, I lose the validation on the dataform. All the required fields are not seen as required. If I use the pcv.CurrentItem as my dataform's CurrentItem validation works fine, but then another issue arrises.

When I use the PagedCollectionView's current item as the dataform's current item:

A user selects an item in the datagrid and the object is loaded fine in the dataform. If a user changes a certain value in any of the textfields on the dataform and then selects a different item to load the dataform with, the following error is thrown:

"Cannot change currency when an item has validation errors or it is being edited and AutoCommit is false. Set ItemsSource to a ICollectionView to manage currency instead." I am not using the paging properties of the dataform and I have my own save button on the form.

I would appreciate any help, this is my first silverlight project that I am working on.

Edit- I used dataform.CommitEdit when changing the dataform's currentitem. One thing that this did not resolve is if there is a validation error on the form, the currency error is thrown. Is there anyway to bypass this. AutoEdit is true and AutoCommit is false for the dataform

It's a bit hard to determine exactly what's going on here without a sample, but here's an observation that may help resolve the problem. Try instead to bind the ItemsSource property of both the DataGrid and the DataForm to the collection view, and don't bind the DataForm's CurrentItem property. They're magically kept in sync (the selected item in the DataGrid will set the current item in the DataForm) - this is a feature of the CollectionView. This may or may not solve your problem, but either way it won't hurt :).

Blatant self promotion: this and other features of the CollectionView are covered in my book Pro Business Applications with Silverlight 4 :).

I need to create silverlight application where customer will see some pdf files. PDF files have to be inside silverlight control and not rendered as images (customer wants to select text)

For this purposes i need some free libriaries or code to convert pdf file to xaml (or just open pdf so i can convert it to xaml).

Which library can read pdf and help me to convert data to xaml? Can I read somehow pdf file and write custom convertation tool?

I saw iTextSharp. Is this library can read pdf and help me with my issue?

I will be thankfull for any ideas or links.

I make use of the Acrobat Reader plugin to do the displaying for me. It does require a different method depending on whether your application is running inside or outside the browser (I check if the application is running inside the browser and change the means of display accordingly). If running inside the browser, I overlay the application with an IFrame, as I describe in this article: Otherwise, I use the WebBrowser control. I have a control which does this all for you in the source code that accompanies my book, which is downloadable from the Apress website here:

Hope this helps...


Is there a way in Silverlight 4 to dictate that all elements within a StackPanel must have a margin, instead of specifying margin="10,0" on each one?

The way I'd do it is by defining implicit styles in the StackPanel's resources, for each control type that will be used within the StackPanel. To save defining the value repeatedly for each control type, you can create a named base style that targets FrameworkElement and defines the style(s), from which the style for each control type can inherit. An example is below:

<StackPanel Orientation="Horizontal">
        <Style x:Key="CommonStyle" TargetType="FrameworkElement">
            <Setter Property="Margin" Value="10,0" />
        <Style TargetType="Button" BasedOn="{StaticResource CommonStyle}" />
        <Style TargetType="TextBlock" BasedOn="{StaticResource CommonStyle}" />
        <Style TargetType="CheckBox" BasedOn="{StaticResource CommonStyle}" />

    <TextBlock Text="Text" />
    <CheckBox>Check Box</CheckBox>

Note how each control in the StackPanel will have the margin applied, without needing to define it on each control.

Hope this helps...

Chris Anderson

PS. Blatant self promotion - this is based upon the inheritance trick in my book Pro Business Applications with Silverlight 4 :).

How does one pass a complex object to the target Page using the NavigationService.Navigate method?

Unfortunately you can't do that. It kind of makes sense, because the idea is to provide deep linking support for pages/views, but it's definitely annoying that you can't do it. The options you have are:

  • For small objects, you could serialise them and pass them to the next view in the query string, although I'd recommend against that approach (different browsers have different number of maximum characters in a URL that they support, and also the object may be out of date if the user bookmarks that page and returns to it).

  • Store the object in a global cache, from which the view being navigated to can access it. Not nice, but it will work.

  • The Navigation Framework source code is a part of the Silverlight Toolkit. You could modify this to support complex objects, but I'd strongly recommend against doing so.

  • Use the MVVM pattern, with one view model used to manage multiple views, and therefore the object would be available to all those views.

Hope this helps...


P.S. I discuss this in my book Pro Business Applications with Silverlight 4, although only in as much depth as above as there's not a particularly nice solution to the problem :).