Seven Silverlight snippets you can not live without

One of the best Visual Studio features is the ability to create code snippets. During our Silverlight Training we use many of them. Here is a selection of the ones we use the most. You can download a zipfile with snippets and copy them to:

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Visual C#

or

c:\Users\You\Documents\Visual Studio 2010\Code Snippets\Visual C#\

1 clsObservableObject

Creates an ObservableObject class you can use to subclass your Models from. Each model that subclasses ObservableObject can call a typed or untyped OnPropertyChanged method.

Output code:

public class ObservableObject : INotifyPropertyChanged {
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName) {
        var handler = PropertyChanged;
        if (handler != null) {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    protected virtual void OnPropertyChanged<TViewModel>(Expression<Func<TViewModel> property) {
        var expression = property.Body as MemberExpression;
        var member = expression.Member;

        if (PropertyChanged != null) {
            PropertyChanged(this, new PropertyChangedEventArgs(member.Name));
        }
    }
}

2 event

Event declaration and a helper method to raise it.

Output code:

public event EventHandler DoubleClick;

protected virtual void OnDoubleClick() {
    EventHandler handler = DoubleClick;
    if (handler != null) {
        handler(this, EventArgs.Empty);
    }
}

3 loremIpsum

Creates a string with lorem ipsum text.

Output code:

var loremIpsum="Lorem ipsum dolor sit amet, Toverstudio consectetuer...";

4 propds

Creates a Silverlight dependency property

Output code:

public string Text {
    get { return (string)GetValue(TextProperty); }
    set { SetValue(TextProperty, value); }
}

/// <summary>
/// Identifies the Text dependency property. This enables animation, styling, binding, etc...
/// </summary>
public static readonly DependencyProperty TextProperty =
    DependencyProperty.Register("Text",
                                typeof(string),
                                typeof(MyControl),
                                new PropertyMetadata(null, OnTextPropertyChanged));

/// <summary>
/// Text changed handler.
/// </summary>
/// <param name="d">MyControl that changed its Text.</param>
/// <param name="e">DependencyPropertyChangedEventArgs.</param>
private static void OnTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
    var source = d as MyControl;
    if (source != null) {
        var value = (string)e.NewValue;
        //TODO: Handle new value.
    }
}

5 propds_nocallback

Creates a Silverlight dependency property without a callback method

Output code:

public string Caption {
    get { return (string)GetValue(CaptionProperty); }
    set { SetValue(CaptionProperty, value); }
}

public static readonly DependencyProperty CaptionProperty =
    DependencyProperty.Register("Caption",
                                typeof(string),
                                typeof(MyControl),
                                new PropertyMetadata(null));

6 propNotify

Uses the OnPropertyChanged with a string parameter (“”) from ObservableObject.

Output code:

private string name;

public string Name {
    get { return name; }
    set {
        if (name != value) {
            name = value;
            OnPropertyChanged("Name");
        }
    }
}

7 propNotify_typed

Uses the typed OnPropertyChanged method from ObservableObject.

Output code:

private string name;
public string Name {
    get { return name; }
    set {
        if (name != value) {
            name = value;
            OnPropertyChanged(() => this.Name);
        }
    }
}