Design time Shadow Property of a Control Container

M

Mark Olbert

I have a UserControl ("CustomContainer") to which I am adding instances of another custom control
("CustomTextBox"). When the CustomTextBox instance accesses a particular property of the
CustomContainer it's on, it needs to be "redirected" to a "shadow" property maintained by the
CustomContainer's designer ("CustomContainerDesigner").

Unfortunately, the CustomTextBox keeps referring to the CustomContainer's "version" of the
property... and I don't understand why.

Here're snippets showing what I've done:

// this designer is referenced via a Designer attribute on the CustomContainer class
// it is marked as being typeof(IRootDesigner) [nothing else seems to actually associate it with
// the CustomContainer's design surface]

public class CustomContainerDesigner : DocumentDesigner
{

// this is the "shadow" property
public SqlDataPackage SqlDataPackage
{
get
{
if( thePkg == null )
{
if( ((CustomContainer) this.Component).SqlDataPackageType != null )
{
// theCtl is set to be the CustomContainer instance

ConstructorInfo dpCtor = theCtl.SqlDataPackageType.GetConstructor(System.Type.EmptyTypes);
thePkg = (SqlDataPackage) dpCtor.Invoke(null);
}
}

return thePkg;
}

set { thePkg = value; }
}

protected override void PreFilterProperties( IDictionary properties )
{
// replace the RecordBase's SqlDataPackage property with a shadow
// property of the same name provided by this designer
base.PreFilterProperties(properties);

PropertyDescriptor prop = TypeDescriptor.CreateProperty(this.GetType(), "SqlDataPackage",
typeof(SqlDataPackage), new Attribute[] { new BrowsableAttribute(false) });

// this should replace the CustomComponent's SqlDataPackage property with
// the shadow property

properties.Remove("SqlDataPackage");
properties.Add("SqlDataPackage", prop);
}
}

public class CustomTextBox
{
public SqlDataPackage SqlDataPackage
{
get
{
// ***********************************
// this is where I want to access the shadow property
// ***********************************
if( !(this.Parent is CustomContainer) ) return null;

return ((CustomContainer) this.Parent).SqlDataPackage;
}

set
{
// no op
}
}
}

I'm wondering if the problem is that the expression this.Parent (which is a reference to the
CustomContainer that my CustomTextBox instance is on) >>doesn't<< refer to the instance of
CustomContainer that's being designed, so the shadow property never gets accessed. If that's the
case, how do I provide shadow properties of a control container to the controls that are in the
container?

Any and all suggestions welcome!

- Mark
 
Y

Ying-Shen Yu[MSFT]

Hi mark,

To my understanding, modify properties collection only affects th
PropertyDescriptor collection, so if you invoke the property directly in
code, you will get the original property, since it's executed by CLR.


To get the shadowed property in the child property, you may use

TypeDescriptor.GetProperties(this.Parent,false)["PropertyName"].GetValue(thi
s.Parent)

to get the shadowed property.

Does it resolve your problem?


Best wisthes,

Ying-Shen Yu [MSFT]
Microsoft community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 
M

Mark Olbert

Ying-shen,

Thanx for the quick response. Your solution is much more clever than mine, which involves creating a
service which I then call with GetService().

- Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top