views:

732

answers:

3

I've created a generic lookless control with virtual property:

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);
        }
    }
}

Then I've made a control derived from it and overrided Value property:

public class TestControl : TestControlBase<int>
{
    public override int Value
    {
        get
        {
            return base.Value;
        }
        set
        {
            base.Value = value;
        }
    }
}

So I use it in a Window XAML:

    <TestControls:TestControl />

When I open window in designer all is OK, but when I put mouse cursor to this line, or to this control in designer I receive exception:

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()

Who know this problem? Please explain :) I have no ideas except that WPF Designer doesn't like generics. If I replace generics by Object all is OK.

A: 

I think there is nothing you can do. It's just a bug. Same thing happens when developing WF controls.

My colleague from work who is developing windows forms controls makes second control which is not generic. It's just a workaround but it works. If you want to work in designer just comment generic (<>), after your work is done, uncomment it.

I hope it will help you :)

Jarek
+1  A: 

Ivan,

Maybe the answer is a little bit late to you but other people can use it too. I had the same problem and got very disappointed when I read that this is a bug. But after some googleing I found a blog that shows a way to use this kind of inheritance. It seems that the problem is solved including some properties in the xaml. I hope it helps.

GRGodoi
A: 

This ist quite old, but I (sort of) stumbled upon it...
I'm not sure, but you're setting a not nullable dependency property without a default value.
I see no way to determine the value of an unset property. I imagine there lies the porblem.

Nils