Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

Enum as DepedencyProperty RSS

14 replies

Last post Oct 14, 2009 10:08 AM by rankorn

(1)
  • DJanjicek

    DJanjicek

    Participant

    1575 Points

    487 Posts

    Enum as DepedencyProperty

    Apr 23, 2009 02:54 PM | LINK

    HI,

    can somebody tell me how to register a dependency property of type enum? My property is accessable in xaml, but not how you would expect from an enum (drop down, etc). And the runtime throws a weird exception which doesn't make much sense...

    Thanks in advance...

  • wjchristenson2

    wjchristenson2

    Member

    104 Points

    51 Posts

    Re: Enum as DepedencyProperty

    Apr 23, 2009 04:17 PM | LINK

    You will need to create a TypeConverter for your property so that it will know how to convert from a string to your enumerated type.  I wrote a quick demo for you on my blog.  The link is below:

    http://www.mostlydevelopers.com/blog/post/2009/04/23/Set-an-Enum-Dependency-Property-with-XAML.aspx

    Bill - Senior .NET Developer
    A Mostly Developers Blogger

    Please remember to click “Mark as Answer” on the post that helps you.
  • DJanjicek

    DJanjicek

    Participant

    1575 Points

    487 Posts

    Re: Enum as DepedencyProperty

    Apr 23, 2009 04:28 PM | LINK

    Thank you very much, i will try that tomorrow! i'm just curious, your solution dictates to have a particular converter for every enum i will work with. How much work would it be to implement a generic converter? If i look at all the out-of-the-box controls which actually use enums for their properties i doubt the MS guys have written a converter for every particular enum they're using.

  • wjchristenson2

    wjchristenson2

    Member

    104 Points

    51 Posts

    Re: Enum as DepedencyProperty

    Apr 23, 2009 04:41 PM | LINK

    I'm sure you could create a generic enum converter. M$ may have one out there that I'm not aware of.  I've not done a lot of searching on it.  Please post if you find one.

    Bill - Senior .NET Developer
    A Mostly Developers Blogger

    Please remember to click “Mark as Answer” on the post that helps you.
  • DJanjicek

    DJanjicek

    Participant

    1575 Points

    487 Posts

    Re: Enum as DepedencyProperty

    Apr 23, 2009 04:52 PM | LINK

    I've found this article on the net, maybe it can be used to extend your implementation...

    http://www.kindblad.com/2008/08/18/how-to-convert-from-string-to-enum-using-c-and-generics/

  • wjchristenson2

    wjchristenson2

    Member

    104 Points

    51 Posts

    Re: Enum as DepedencyProperty

    Apr 23, 2009 07:58 PM | LINK

    Below is a generic EnumTypeConverter class i wrote. 

    Here's a property using it:

        <TypeConverter(GetType(EnumTypeConverter(Of Vehicle.VehicleType)))> _
        Public Property Type() As VehicleType
            Get
                Return DirectCast
    (GetValue(Vehicle.TypeProperty), VehicleType)
            End Get
            Set
    (ByVal value As VehicleType)
                SetValue(Vehicle.TypeProperty, value)
            End Set
        End Property

    Here is the generic EnumTypeConverter:

    Imports System.ComponentModel
    
    Public Class EnumTypeConverter(Of T)
        Inherits TypeConverter
    
        Public Overrides Function CanConvertFrom(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal sourceType As System.Type) As Boolean
            If sourceType.Equals(GetType(String)) Then
                Return True
            Else
                Return MyBase.CanConvertFrom(context, sourceType)
            End If
        End Function
    
        Public Overrides Function CanConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal destinationType As System.Type) As Boolean
            If destinationType.Equals(GetType(String)) Then
                Return True
            Else
                Return MyBase.CanConvertTo(context, destinationType)
            End If
        End Function
    
        Public Overrides Function ConvertFrom(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object) As Object
            If TypeOf value Is String Then
                Try
                    Return CType([Enum].Parse(GetType(T), value, True), T)
                Catch
                    Throw New InvalidCastException(value)
                End Try
            Else
                Return MyBase.ConvertFrom(context, culture, value)
            End If
        End Function
    
        Public Overrides Function ConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object, ByVal destinationType As System.Type) As Object
            If destinationType.Equals(GetType(String)) Then
                Return value.ToString()
            Else
                Return MyBase.ConvertTo(context, culture, value, destinationType)
            End If
        End Function
    End Class
     
    Bill - Senior .NET Developer
    A Mostly Developers Blogger

    Please remember to click “Mark as Answer” on the post that helps you.
  • DJanjicek

    DJanjicek

    Participant

    1575 Points

    487 Posts

    Re: Enum as DepedencyProperty

    Apr 23, 2009 08:09 PM | LINK

    I'll post the c# version tomorrow! Thanks for your effort! [Y]

  • DMW

    DMW

    Member

    120 Points

    20 Posts

    Re: Enum as DepedencyProperty

    Apr 24, 2009 02:06 AM | LINK

    DJanjicek

    i doubt the MS guys have written a converter for every particular enum they're using
    Your are quite correct; they have not. The XAML parser can automatically parse strings to named enum values, and if you examine an enum-based property in Blend, you will see that it automatically displays a drop-down for the property. You therefore do not need to use a TypeConverter, and doing so will achieve little for a simple enum. I'm not denying that you might have had problems, but a TypeConverter is not the solution to them.
    Regards

    Dave
  • DMW

    DMW

    Member

    120 Points

    20 Posts

    Re: Enum as DepedencyProperty

    Apr 24, 2009 02:11 AM | LINK

    A further thought has crossed my mind. A common mistake to make when registering DPs is to leave in the default PropertyMetadata(0) in the DP.Register method. This will result in a TypeInitializationException at runtime, and potentially strange problems at design-time. Make sure that you change this to a proper value of the enum, as the PropertyMetadata constructor takes a parameter of type object. Thus boxing will occur, and unboxing an Int32 to an enum will fail during type initialization.
    Regards

    Dave
  • DJanjicek

    DJanjicek

    Participant

    1575 Points

    487 Posts

    Re: Enum as DepedencyProperty

    Apr 24, 2009 06:33 AM | LINK

    dmw

    A further thought has crossed my mind. A common mistake to make when registering DPs is to leave in the default PropertyMetadata(0) in the DP.Register method. This will result in a TypeInitializationException at runtime, and potentially strange problems at design-time. Make sure that you change this to a proper value of the enum, as the PropertyMetadata constructor takes a parameter of type object. Thus boxing will occur, and unboxing an Int32 to an enum will fail during type initialization.

    I don't understand this one... Could you provide an example?