Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Enum as DepedencyProperty
14 replies. Latest Post by rankorn on October 14, 2009.
(1)
DJanjicek
Participant
964 points
319 Posts
04-23-2009 10:54 AM |
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...
wjchrist...
Member
104 points
51 Posts
04-23-2009 12:17 PM |
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
04-23-2009 12:28 PM |
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.
04-23-2009 12:41 PM |
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.
04-23-2009 12:52 PM |
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/
04-23-2009 3:58 PM |
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
04-23-2009 4:09 PM |
I'll post the c# version tomorrow! Thanks for your effort!
dmw
106 points
18 Posts
04-23-2009 10:06 PM |
DJanjicek:i doubt the MS guys have written a converter for every particular enum they're using
04-23-2009 10:11 PM |
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.
04-24-2009 2:33 AM |
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
04-24-2009 5:06 AM |
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.
04-24-2009 11:53 AM |
Okay, so i have to create for every enum value a property?
04-24-2009 12:30 PM |
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.
04-24-2009 12:49 PM |
It works! Still a pity that IntelliSense doesn't work...
Anyways, thank you!
rankorn
2 points
4 Posts
10-14-2009 6:08 AM |
In SL3, you do not even have to use TypeConverter as it automatically handles enums. I am not sure about intellisense.