我用虚拟属性创建了一个通用的无外观控件:
public abstract class TestControlBase<TValue> : Control
{
public static readonly DependencyProperty ValueProperty;
static TestControlBase()
{
ValueProperty = DependencyProperty.Register("Value", typeof(TValue),
typeof(TestControlBase<TValue>));
}
protected TestControlBase()
{
Focusable = false;
Value = default(TValue);
}
public virtual TValue Value
{
get
{
return (TValue)GetValue(ValueProperty);
}
set
{
SetValue(ValueProperty, value);
}
}
}
然后我创建了一个从它派生的控件并覆盖了 Value 属性:
public class TestControl : TestControlBase<int>
{
public override int Value
{
get
{
return base.Value;
}
set
{
base.Value = value;
}
}
}
所以我在 Window XAML 中使用它:
<TestControls:TestControl />
当我在设计器中打开窗口时一切正常,但是当我将鼠标光标放在这一行或设计器中的这个控件时,我收到异常:
Exception has been thrown by the target of an invocation.
at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
Ambiguous match found.
at System.RuntimeType.GetPropertyImpl(String name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
at System.Type.GetProperty(String name)
at MS.Internal.ComponentModel.DependencyPropertyKind.get_IsDirect()
at MS.Internal.ComponentModel.DependencyPropertyKind.get_IsAttached()
at MS.Internal.ComponentModel.APCustomTypeDescriptor.GetProperties(Attribute[] attributes)
at MS.Internal.ComponentModel.APCustomTypeDescriptor.GetProperties()
at System.ComponentModel.TypeDescriptor.TypeDescriptionNode.DefaultExtendedTypeDescriptor.System.ComponentModel.ICustomTypeDescriptor.GetProperties()
at System.ComponentModel.TypeDescriptor.GetPropertiesImpl(Object component, Attribute[] attributes, Boolean noCustomTypeDesc, Boolean noAttributes)
at System.ComponentModel.TypeDescriptor.GetProperties(Object component)
at MS.Internal.Model.ModelPropertyCollectionImpl.GetProperties(String propertyNameHint)
at MS.Internal.Model.ModelPropertyCollectionImpl.<GetEnumerator>d__0.MoveNext()
at MS.Internal.Designer.PropertyEditing.Model.ModelPropertyMerger.<GetFirstProperties>d__0.MoveNext()
at MS.Internal.Designer.PropertyEditing.PropertyInspector.UpdateCategories(Selection selection)
at MS.Internal.Designer.PropertyEditing.PropertyInspector.OnSelectionChangedIdle()
谁知道这个问题?请解释一下 :) 除了 WPF Designer 不喜欢泛型之外,我没有任何想法。如果我全部替换泛型Object
就可以了。