def*_*mer 2 data-binding wpf customtypedescriptor inotifypropertychanged icustomtypedescriptor
我正在尝试使用它[TypeDescriptionProviderAttribute]
来为我的类提供自定义类型描述符.这是有效的,但是当我实现INotifyPropertyChanged
WPF时,似乎忽略了自定义类型描述符并直接进入CLR属性(如果存在).这是一个片段,稍后我将粘贴完整的示例:
//[TypeDescriptionProvider(typeof(MyProvider))]
class MyModel : Object
//, INotifyPropertyChanged
//, ICustomTypeDescriptor
{
public string TheProperty { get { return "CLR - TheProperty"; } }
Run Code Online (Sandbox Code Playgroud)
我将TextBlock绑定到TheProperty.当我...
留下所有评论
我按预期看到了"CLR - TheProperty".
使用 [TypeDescriptionProvider]
我按预期看到了"MyPropertyDescriptor - TheProperty".
使用 ICustomTypeDescriptor
我按预期看到了"MyPropertyDescriptor - TheProperty".
使用ICustomTypeDescriptor
和INotifyPropertyChanged
我按预期看到了"MyPropertyDescriptor - TheProperty".
使用[TypeDescriptionProvider]
和INotifyPropertyChanged
我看到"CLR - TheProperty".为什么是这样?奇怪的是,正常显示没有 CLR属性的自定义属性.我的自定义类型描述符也返回"MyPropertyDescriptor - AnotherProperty",它在所有情况下都有效,因为没有AnotherProperty
定义CLR .
总之,鉴于此XAML
<StackPanel>
<TextBlock Text="{Binding TheProperty}" />
<TextBlock Text="{Binding AnotherProperty}" />
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
AnotherProperty
始终按预期工作,因为模型没有名为"AnotherProperty"的CLR属性.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;
}
}
Run Code Online (Sandbox Code Playgroud)
老问题,但对于寻找答案的人来说..
问题出在System.Windows.PropertyPath.ResolvePropertyName(String,Object,Type,Object,Boolean)私有方法中.我在.NET 4.0中的PresentationFramework.dll中找到了它.
从.NET Reflector中提取:
object propertyHelper = DependencyProperty.FromName(str, ownerType);
if ((propertyHelper == null) && (item is ICustomTypeDescriptor))
{
propertyHelper = TypeDescriptor.GetProperties(item)[str];
}
if ((propertyHelper == null) && ((item is INotifyPropertyChanged) || (item is DependencyObject)))
{
propertyHelper = this.GetPropertyHelper(ownerType, str);
}
if (propertyHelper == null)
{
propertyHelper = TypeDescriptor.GetProperties(item)[str];
}
if (propertyHelper == null)
{
propertyHelper = this.GetPropertyHelper(ownerType, str);
}
if ((propertyHelper == null) && throwOnError)
{
throw new InvalidOperationException(SR.Get("PropertyPathNoProperty", new object[] { ownerType.Name, str }));
}
return propertyHelper;
Run Code Online (Sandbox Code Playgroud)
如您所见,检索属性标识符(DependencyProperty/PropertyDescriptor/PropertyInfo)如下所示:
因此,如果item实现了INotifyPropertyChanged接口,则System.Reflection/PropertyInfo优先于TypeDescriptor/PropertyDescriptor.我认为他们出于性能原因选择了这种策略,因为PropertyInfo比PropertyDescriptor轻得多.
您的问题的解决方案是实现ICustomTypeDescriptor(最好是显式的),以便它将ICustomTypeDescriptor方法调用传递给适当的TypeDescriptor方法调用,但不传输对象参数,但是Type参数(this.GetType()).这样就可以使用TypeDescriptionProvider.
归档时间: |
|
查看次数: |
1656 次 |
最近记录: |