in

Dé specialist in .NET trainingen en consultancy

Erik van Appeldoorn

februari 2009 - Posts

  • Dependency properties in Silverlight

    Dependency properties were some time ago introduced in WPF and are widely used in Silverlight 2 controls as well. They are in effect in e.g. animations, data-binding and styles. I'm sure you use them when creating a Silverlight 2 application.  The need to write your own dependency properties will come when you decide to create your own Silverlight control. I'm not going to explain the dependency property concept here, you can find some good documentation on msdn.

    The syntax is something you have to get used to, typically you will see something like this: (code includes the dependency property, a clr-wrapper for this property and a property-changed callback.

       1:      public class AppelControl : ContentControl
       2:      {
       3:   
       4:          public bool IsTest
       5:          {
       6:              get { return (bool)GetValue(IsTestProperty); }
       7:              set { SetValue(IsTestProperty, value); }
       8:          }
       9:   
      10:          public static readonly DependencyProperty IsTestProperty =
      11:                      DependencyProperty.Register(
      12:                            "IsTest",
      13:                            typeof(bool),
      14:                            typeof(AppelControl),
      15:                            new PropertyMetadata(OnIsTestPropertyChanged));
      16:   
      17:          private static void OnIsTestPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
      18:          {
      19:              AppelControl _AppelControl = d as AppelControl;
      20:              if (_AppelControl != null)
      21:              {
      22:                  //TODO: Handle new value. 
      23:              }
      24:          }
      25:      }

     

    Hmm.. a lot of work for a simple bool property, don't you think? But don't panic, we have codesnippets in Visual Studio, just type: propdp [TAB] and you get the following code:

       1:  public int MyProperty
       2:          {
       3:              get { return (int)GetValue(MyPropertyProperty); }
       4:              set { SetValue(MyPropertyProperty, value); }
       5:          }
       6:   
       7:          // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
       8:          public static readonly DependencyProperty MyPropertyProperty =
       9:              DependencyProperty.Register("MyProperty", typeof(int), typeof(ownerclass), new UIPropertyMetadata(0));

     

    We ran into a small problem here, the codesnippet shipped is designed to work with WPF and not with Silverlight 2, this will not compile!

    You can get a Silverlight 2 dependency property codesnippet over here. Just import this snippet and you can use propds [TAB] to get a full blown Silverlight 2 dependency property with just a few keystrokes.

     

     

     

  • Engines started with Silverlight 2

    At Oosterkamp Thomas and I are huge Formula One fans. For years now I'm running a F1 game for some F1 addicted friends of me. This year I decided to change the rules of the game a little bit and create some functionallity on the web so users can make changes to their predictions using a site rather than send me some e-mail. You can see a beta preview of the game site at http://www.independentf1.nl (log in using test | test)

    Some technologies I used for this site are: .NET Framework 3.5, SQL Server 2005, Linq-to-SQL, Silverlight enabled WCF and Silverlight 2. I used Microsoft Expression Blend for the Silverlight lay-out and animations. Creating simple animations based on timelines and adding media is super easy using Blend. Although I have to confess I'm absolutely no designer, so don't judge me for that.

    Feel free to drop me mail if you like to start using Silverlight but don't know where to start or need some advise. If you like to join the game this year send a mail to info@independentf1.nl and I will send you the rules later on.