Skip to main content

Microsoft Silverlight

Answered Question Enum as DepedencyPropertyRSS Feed

(1)

DJanjicek
DJanjicek

Participant

Participant

964 points

319 Posts

Enum as DepedencyProperty

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
wjchrist...

Member

Member

104 points

51 Posts

Re: Enum as DepedencyProperty

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

Participant

964 points

319 Posts

Re: Enum as DepedencyProperty

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
wjchrist...

Member

Member

104 points

51 Posts

Re: Enum as DepedencyProperty

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

Participant

964 points

319 Posts

Re: Enum as DepedencyProperty

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
wjchrist...

Member

Member

104 points

51 Posts

Answered Question

Re: Enum as DepedencyProperty

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

Participant

964 points

319 Posts

Re: Enum as DepedencyProperty

I'll post the c# version tomorrow! Thanks for your effort! Yes

dmw
dmw

Member

Member

106 points

18 Posts

Moderator

Re: Enum as DepedencyProperty

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

Member

106 points

18 Posts

Moderator

Re: Enum as DepedencyProperty

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

Participant

964 points

319 Posts

Re: Enum as DepedencyProperty

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?

DMW
DMW

Member

Member

106 points

18 Posts

Moderator

Re: Enum as DepedencyProperty

In the call to DependencyProperty.Register, make sure that the last parameter reads something like this new PropertyMetadata( Foo.A ) assuming that you have an enum called Foo, with a named value A.

Regards

Dave

DJanjicek
DJanjicek

Participant

Participant

964 points

319 Posts

Re: Enum as DepedencyProperty

Okay, so i have to create for every enum value a property?

dmw
dmw

Member

Member

106 points

18 Posts

Moderator

Re: Enum as DepedencyProperty

No, absolutely not. You can expose a single DP, whose type is the type of the enum. Then you will be able to set the value of that property to any of the values of the enum.

Regards

Dave

DJanjicek
DJanjicek

Participant

Participant

964 points

319 Posts

Re: Enum as DepedencyProperty

It works! Still a pity that IntelliSense doesn't work...

Anyways, thank you!

rankorn
rankorn

Member

Member

2 points

4 Posts

Re: Enum as DepedencyProperty

In SL3, you do not even have to use TypeConverter as it automatically handles enums. I am not sure about intellisense.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities