A WP7 databound graph control

For a project we are working on, we needed a simple (and free) WP7 graph usercontrol. The data which the usercontrol visualizes needs to be databound and each time new data arrives, the graph should update itself on-the-fly .In the following tutorial we’ll show how to create such a control from scratch (make note that normally this should also work in Silverlight or WPF projects).

A demo-solution of this tutorial can be downloaded here.

Read more of this post

Creating an Intellisense compatible enum-based Dependency Property

Actually, this post has a way too fancy title , because in fact I will merely show that enum-based dependency properties are IntelliSense compatible “out-of-the-box”. (by the way, if you know all about dependcy properties: simply read the line in bold and you’ll know all there is to know).

When creating a (WP7/SL/WPF) usercontrol, one often ends up creating one more dependency properties (DP). Most of the times you only want a discrete set of possible values that can be assigned to the DP. The logical choice then of course is to have an enum-based DP.

Now, for the Intellisense to work it is important that you define the enum type OUTSIDE the usercontrols class. For example, suppose we have define the following enum:

public enum GraphTypes {Default, Point, Line}

Now, all that remains is to add a DP that uses this enum (remember that you can use the ‘dependencyproperty’ snippet that comes with VS):

        public static readonly DependencyProperty GraphTypeProperty =
            DependencyProperty.Register("GraphType", typeof (GraphTypes), typeof (GraphControl), new PropertyMetadata(GraphTypes.Default));

        public GraphTypes GraphType
        {
            get { return (GraphTypes) GetValue(GraphTypeProperty); }
            set { SetValue(GraphTypeProperty, value); }
        }

Once you now add the usercontrol elsewhere in your xaml-code, IntelliSense will happily show what values can be assigned to the DP:

There we go. That’s all there was too it.

Next post I’ll show how to create a WP7 user control to plot graphs using data binding. Consider some of the code here a sneak preview.

Tutorial on Silverlight 4 databinding in code-behind, custom user controls, etc.

Introduction

This small tutorial was written to show the students the following aspects of Silverlight:

  • Writing a class that can be used for databinding
  • Perform data-binding through code instead of XAML
  • Creating a custom user control
  • Writing simple data converters

Suppose we are creating a Silverlight game in which each player is represented as a pawn. However, the player class itself is somewhere deep inside the game-engine and we would like the pawn user control to be only loosely coupled to this player class. By doing this, we are able to make a rapid Silverlight prototype and if we later decide that the frontend is pretty lame, we can simply redesign it without too much fuss.

Read more of this post