views:

857

answers:

4

I have an extender (IExtenderProvider) which extends certain types of controls with additional properties. For one of these properties, I have written a UITypeEditor. So far, all works just fine.

The extender also has a couple of properties itself, which I am trying to use as a sort of default for the UITypeEditor. What I want to do is to be able to set a property on the extender itself (not the extended controls), and when I open up the UITypeEditor for one of the additional properties on an extended control, I want to set a value in the UITypeEditor to the value of the property on the extender.

A simple example: The ExtenderProvider has a property DefaultExtendedValue. On the form I set the value of this property to "My Value". Extended controls have, through the provider, a property ExtendedValue with a UITypeEditor. When I open the editor for the property ExtendedValue the default (initial) value should be set to "My Value".

It seems to me that the best place to do this would be UITypeEditor.EditValue, just before calling IWindowsFormsEditorService.DropDownControl or .ShowDialog.

The only problem is that I can't (or I haven't discovered how to) get hold of the extender provider itself in EditValue, to read the value of the property in question and set it in the UITypeEditor. Context gives me the extended control, but that is of no use to me in this case.

Is there any way to achieve what I'm trying? Any help appreciated!

Thanks Tom

A: 

Have you considered adding the DefaultValue as a static property of the ExtenderProvider, then you can access it without requiring an instance of the provider?

samjudson
A: 

@samjudson: That's not a bad idea, but unfortunately it doesn't quite get me there. I'd really like to be able to set this default value individually for each instance of the extender provider. (I might have more than one on a single form with different values for different groups of extended controls.)

Tom Juergens
+2  A: 

Could you read the attribute yourself?

DefaultValueAttribute att = context.
    PropertyDescriptor.Attributes.
    OfType<DefaultValueAttribute>().
    FirstOrDefault();
object myDefault = null;
if ( att != null )
    myDefault = att.Value;

I've used Linq to simplify the code, but you could do something similar back in .Net 1

Keith
A: 

Hi I have found this : http://social.msdn.microsoft.com/forums/en-US/winformsdesigner/thread/07299eb0-3e21-42a3-b36b-12e37282af83/

Basically :

var Ctl = context.Instance as Control;

Type t = Type.GetType("System.ComponentModel.ExtendedPropertyDescriptor");
LocalizationProvider myProvider = GetValueOnPrivateMember(t, context.PropertyDescriptor, "provider") as MyOwnExtenderProvider;

And magically, myProvider got my IExtenderProvider control !

where GetValueOnPrivateMember should be implemented this way:

static object GetValueOnPrivateMember(Type type, object dataobject, string fieldname)
        {
            BindingFlags getFieldBindingFlags = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField;
            return type.InvokeMember(fieldname,
                getFieldBindingFlags,
                null,
                dataobject,
                null);
        }
controlbreak