DesignData support for Silverlight in Visual Studio 2010 and Blend 4

by Vlad 14. May 2010 12:52

In order to take advantage of the design-time binding support in Blend (and now VS .NET 2010), especially when using MVVM, you had these options:

1. Set the DataContext in XAML to a StaticResource (like a ViewModel) created for design only, and change it to the real thing at runtime.

Good: use the existing class structure, XAML intellisense and autocompletion.

Bad: the design-time object will get created at runtime too, cannot access private set or readonly properties, must have a parameterless constructor.

2. Create sample data in Blend (XML).

Good: can be created by designers, can use private set or readonly properties, no runtime penalty.

Bad: must be kept in sync with the actual ViewModel, no compile-time checking, no intellisense support.

DesignData

The DesignData combines the advantages of the two options - you can create a XAML with your design values, based on the existing class model.

You can take advantage of intellisense and autocompletion and you're able to set readonly properties, instantiate classes with no parameterless contructors.

The objects will be created only at design time, so there is no runtime penalty. You also get compile-time checking - you get an error if you try to set an invalid value to a int property, for example.

This functionality has been available since Blend 3, but it was (and still is) kind of hidden - in Blend 3 / VS 2008 you had to manually edit the project file to get it to work.

Fortunately in VS 2010 you don't need to edit the project file, but there still is no Item Template for DesignData files.

More...

Categories: MVVM | Silverlight | DesignData

Syntax Highlighting TextBox in Silverlight using MVVM

by Vlad 5. May 2010 19:53

This is a demo about leveraging the databinding and templating support in Silverlight and the MVVM pattern to create a simple syntax highlighting textbox control.

The idea is to have an invisible TextBox (handling the editing) overlapping a ItemsControl that uses TextBlocks to highlight the keywords in the text. The ItemsSource property of the ItemsControl is bound to a collection property on the ViewModel, so we only need to update this collection in order to maintain the controls in sync.

In the end we'll get something like this:

More...