如何在silverlight中使用自定义枚举属性
属性我知道怎么定义,
不过我想要的是在另一个XAML页面调用此自定义控件,
然后可以设置此枚举属性。
- C# code
public enum MyModel { one, two } private MyModel _model;public static readonly DependencyProperty _modelProperty = DependencyProperty.Register("Model", typeof(MyModel), typeof(MyClass), null); public MyModelModel { get { return _model; } set { _model = value; } }
我现在是这样设置的,不过在另一个调用此控件的XAML页面调用不了次属性,
其他基本类型可以调用....
望高手指点..
[解决办法]
参考下面的代码:
- VB.NET code
<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 Imports System.ComponentModelPublic 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 FunctionEnd Class
[解决办法]
顶 拿分。。。。