3

我用虚拟属性创建了一个通用的无外观控件:

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就可以了。

4

3 回答 3

2

伊万,

也许答案对您来说有点晚,但其他人也可以使用它。当我读到这是一个错误时,我遇到了同样的问题并且非常失望。但经过一番谷歌搜索后,我发现了一个博客,其中展示了一种使用这种继承的方法。似乎问题已解决,包括 xaml 中的一些属性。我希望它有所帮助。

于 2009-02-27T00:54:20.803 回答
1

我认为你无能为力。这只是一个错误。开发 WF 控件时也会发生同样的事情。

我正在开发 Windows 窗体控件的同事制作了不通用的第二个控件。这只是一种解决方法,但它有效。如果您想在设计师中工作,只需注释通用 (<>),在您的工作完成后,取消注释它。

我希望它会帮助你:)

于 2008-12-23T20:49:37.550 回答
1

这很旧,但我(有点)偶然发现它......
我不确定,但你正在设置一个没有默认值的不可为空的依赖属性。
我看不到确定未设置属性的值的方法。我想这就是问题所在。

于 2010-04-26T08:50:28.110 回答