如何在silverlight中使用自定义枚举属性

tommir3 2010-03-31 03:07:08
属性我知道怎么定义,
不过我想要的是在另一个XAML页面调用此自定义控件,
然后可以设置此枚举属性。


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页面调用不了次属性,
其他基本类型可以调用....
望高手指点..
...全文
150 4 打赏 收藏 转发到动态 举报
写回复
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wh1007021 2010-11-14
  • 打赏
  • 举报
回复
我也碰到这个问题,我也用DenpencyProperty注册了一个枚举的依赖项属性,具体参照
http://msdn.microsoft.com/zh-cn/library/cc903933(v=VS.95).aspx#whatis
这里来做的,但是没有成功。
不知道楼主是在哪个地方定义的属性,我是写了一个BaseControl类,继承UserControl。然后我想
自定义控件的时候,继承自己的BaseControl类~~接触SilverLight一个星期不到,见笑~~等待答案
jv9 2010-04-01
  • 打赏
  • 举报
回复
参考下面的代码:

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



这里有篇文章也可以参考。

http://silverlightchina.net/html/developer/2010/0331/946.html
tommir3 2010-04-01
  • 打赏
  • 举报
回复
能变成C#代码吗?
VB的看不懂啊......-_-!
程序一员 2010-04-01
  • 打赏
  • 举报
回复
顶 拿分。。。。
相关推荐

8,705

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告