我试图使用[TypeDescriptionProviderAttribute]
为了给我的类定制一个类型描述符。这工作,但是当我实现INotifyPropertyChanged
WPF似乎忽略了自定义类型描述符,并直接为CLR属性(如果存在)。以下是一段代码,稍后我将粘贴完整示例:当INotifyPropertyChanged被实现时,为什么WPF似乎绕过了TypeDescriptionProviderAttribute?
//[TypeDescriptionProvider(typeof(MyProvider))]
class MyModel : Object
//, INotifyPropertyChanged
//, ICustomTypeDescriptor
{
public string TheProperty { get { return "CLR - TheProperty"; } }
我将TextBlock绑定到TheProperty。当我...
让一切评论
我看到的 - 如预期 “CLR利人”。
使用
[TypeDescriptionProvider]
我看到的 - 如预期 “MyPropertyDescriptor利人”。
使用
ICustomTypeDescriptor
我看到的 - 如预期 “MyPropertyDescriptor利人”。
使用
ICustomTypeDescriptor
和INotifyPropertyChanged
我看到的 - 如预期 “MyPropertyDescriptor利人”。
使用
[TypeDescriptionProvider]
和INotifyPropertyChanged
我看到 “CLR - 利人”。 这是为什么?奇怪的是,自定义属性没有 CLR属性正常显示。我的自定义类型描述符还会返回一个“MyPropertyDescriptor - AnotherProperty”,它可以在所有情况下都起作用,因为没有定义CLR
AnotherProperty
。
综上所述,鉴于此XAML预期,因为该模型没有一个名为 “AnotherProperty” 一个CLR属性
<StackPanel>
<TextBlock Text="{Binding TheProperty}" />
<TextBlock Text="{Binding AnotherProperty}" />
</StackPanel>
AnotherProperty
始终有效。 TheProperty
按预期工作除了当[TypeDescriptionProvider]
和INotifyPropertyChanged
都使用。
以下是完整的代码。这是一个有点长,但大部分是不相关的,它只是由System.ComponentModel
public partial class TestWindow : Window
{
public TestWindow()
{
InitializeComponent();
DataContext = new MyModel();
}
}
//[TypeDescriptionProvider(typeof(MyProvider))]
class MyModel : Object
//, INotifyPropertyChanged
//, ICustomTypeDescriptor
{
public string TheProperty { get { return "CLR - TheProperty"; } }
public event PropertyChangedEventHandler PropertyChanged;
public AttributeCollection GetAttributes()
{
return TypeDescriptor.GetAttributes(this);
}
public string GetClassName()
{
return TypeDescriptor.GetClassName(this);
}
public string GetComponentName()
{
return TypeDescriptor.GetComponentName(this);
}
public TypeConverter GetConverter()
{
return TypeDescriptor.GetConverter(this);
}
public EventDescriptor GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(this);
}
public PropertyDescriptor GetDefaultProperty()
{
return TypeDescriptor.GetDefaultProperty(this);
}
public object GetEditor(Type editorBaseType)
{
return TypeDescriptor.GetEditor(this, editorBaseType);
}
public EventDescriptorCollection GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(this, attributes);
}
public EventDescriptorCollection GetEvents()
{
return TypeDescriptor.GetEvents(this);
}
public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
return TypeDescriptor.GetProperties(this, attributes);
}
public PropertyDescriptorCollection GetProperties()
{
return MyTypeDescriptor.GetCustomProperties();
}
public object GetPropertyOwner(PropertyDescriptor pd)
{
return this;
}
}
class MyProvider : TypeDescriptionProvider
{
public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
{
return new MyTypeDescriptor();
}
}
class MyTypeDescriptor : CustomTypeDescriptor
{
public override PropertyDescriptorCollection GetProperties()
{
return GetCustomProperties();
}
public static PropertyDescriptorCollection GetCustomProperties()
{
return new PropertyDescriptorCollection(
new[] {
new MyPropertyDescriptor("TheProperty"),
new MyPropertyDescriptor("AnotherProperty")
});
}
}
class MyPropertyDescriptor : PropertyDescriptor
{
public MyPropertyDescriptor(string propName)
: base(propName, null)
{
}
public override bool CanResetValue(object component)
{
return false;
}
public override Type ComponentType
{
get { return typeof(MyModel); }
}
public override object GetValue(object component)
{
return "MyPropertyDescriptor - " + Name;
}
public override bool IsReadOnly
{
get { return true; }
}
public override Type PropertyType
{
get { return typeof(string); }
}
public override void ResetValue(object component)
{
throw new InvalidOperationException("cannot reset value");
}
public override void SetValue(object component, object value)
{
throw new InvalidOperationException("property is readonly");
}
public override bool ShouldSerializeValue(object component)
{
return true;
}
}
当我试图找出问题背后的原因(以及解决方案以及......)时,我很想知道您究竟在这里实现了什么目标?动态属性? –
@AngelWPF - 用几句话来描述并不容易,但是当我在一两天内写出描述时,我会发布一个链接。 –