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.

Advertisements

About timdams
C#, .NET, Microsoft, security, .... Read more on : https://timdams.com/

4 Responses to Creating an Intellisense compatible enum-based Dependency Property

  1. Pingback: Blog J.Schweiss | WP7 – Databound Chart Control

  2. Rob Ainscough says:

    Couldn’t get the Intellisense to work for me using this approach — a class OUTSIDE of the UserControl or even an enum in a class in a separate library outside of the project?? Are you using ReSharper or something else that might be doing the Intellisense for you?

  3. Rob Ainscough says:

    What you are missing in your code is the Public Shared Function GetEventType and SetEventType … it’s possible you are inheriting these from something else and hence you confusion as to why the Enum must be OUTSIDE the class … it does not.

    Here is correct code sample (in VB):

    Partial Public Class DCButtonMain
    Inherits UserControl

    Public Enum TestEvents
    Errors
    Tables
    System
    TransactionCodes
    Forms
    Communications
    End Enum

    Public Shared ReadOnly EventTypeProperty As DependencyProperty = DependencyProperty.Register(“EventType”, GetType(TestEvents), GetType(DCButtonMain), New PropertyMetadata(TestEvents.Forms))

    Public Shared Function GetEventType(ByVal obj As Button) As TestEvents
    Return CInt(obj.GetValue(EventTypeProperty))
    End Function

    Public Shared Sub SetEventType(ByVal obj As Button, ByVal value As TestEvents)
    obj.SetValue(EventTypeProperty, value)
    End Sub

    Private _EventType As TestEvents
    Public Property EvenType() As TestEvents
    Get
    Return _EventType
    End Get
    Set(ByVal value As TestEvents)
    _EventType = value
    End Set
    End Property

    Public Sub New
    InitializeComponent()
    End Sub

    End Class

    Cheers, Rob

  4. Scott Elliott says:

    It worked for me as well inside the control class. Here is my snippet:

    #region CountryFormat
    public enum CountryFormats : int
    {
    Alpha2Code = 0,
    Alpha3Code = 1,
    Name = 2
    }
    public static readonly DependencyProperty CountryFormatProperty = DependencyProperty.Register
    (
    “CountryFormat”,
    typeof(CountryFormats),
    typeof(CountryAndSubdivisionFilter),
    new PropertyMetadata(CountryFormats.Name)
    );
    public CountryFormats CountryFormat
    {
    get
    {
    return (CountryFormats) GetValue(CountryFormatProperty);
    }
    set
    {
    SetValue(CountryFormatProperty, value);

    switch (value)
    {
    case CountryFormats.Alpha2Code:

    this.CountryAlpha2CodeBox.Visibility = Visibility.Visible;
    this.CountryAlpha3CodeBox.Visibility = Visibility.Collapsed;
    this.CountryNameBox.Visibility = Visibility.Collapsed;

    break;

    case CountryFormats.Alpha3Code:

    this.CountryAlpha2CodeBox.Visibility = Visibility.Collapsed;
    this.CountryAlpha3CodeBox.Visibility = Visibility.Visible;
    this.CountryNameBox.Visibility = Visibility.Collapsed;

    break;

    default:

    this.CountryAlpha2CodeBox.Visibility = Visibility.Collapsed;
    this.CountryAlpha3CodeBox.Visibility = Visibility.Collapsed;
    this.CountryNameBox.Visibility = Visibility.Visible;

    break;
    }
    }
    }
    #endregion

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: