Usercontrol property appears in property grid but not intellisense

B

Bruce Wood

I have an absolutely freakish problem that is driving me nuts.

I created a user control that encapsulates a ListView. I exposed many
of the ListView properties through the user control, something like
this:

[DefaultValue(false), Category("Appearance"), Description("...")]
public bool CheckBoxes
{
get { return this._listView.CheckBoxes; }
set { this._listView.CheckBoxes = value; }
}

This particular property, CheckBoxes, appears in the property grid in
the Designer. If I change its value, the Designer generates the correct
code:

this._listViewForSelfKeyedCollection1.CheckBoxes = true;

However, if I close the Designer window for the user control that I'm
designing (which contains the "list view" user control) and reopen it,
the list view disappears. If I then go into the Designer generated code
and remove that one line, the list view user control reappears on the
designed user control.

If I try to retype the line by hand into the Designer generated code, I
get this far:

this._listViewForSelfKeyedCollection1.

and Intellisense shows me the available methods and properties, but the
CheckBoxes property _isn't on the list_!

There is something freaky about this property: if I don't set it, the
list view user control appears in the Designer window and all is well.
If I do set it, the Designer hates my "list view" user control and
removes it from the user control I'm designing.

Has anyone run across this sort of weird behaviour? What causes it? I'm
completely stumped.
 
H

Herfried K. Wagner [MVP]

Bruce Wood said:
I created a user control that encapsulates a ListView. I exposed many
of the ListView properties through the user control, something like
this:

[DefaultValue(false), Category("Appearance"), Description("...")]
public bool CheckBoxes
{
get { return this._listView.CheckBoxes; }
set { this._listView.CheckBoxes = value; }
}

This particular property, CheckBoxes, appears in the property grid in
the Designer. If I change its value, the Designer generates the correct
code:

this._listViewForSelfKeyedCollection1.CheckBoxes = true;

However, if I close the Designer window for the user control that I'm
designing (which contains the "list view" user control) and reopen it,
the list view disappears. If I then go into the Designer generated code
and remove that one line, the list view user control reappears on the
designed user control.

If I try to retype the line by hand into the Designer generated code, I
get this far:

this._listViewForSelfKeyedCollection1.

and Intellisense shows me the available methods and properties, but the
CheckBoxes property _isn't on the list_!


I doubt that this will fix the problem, but it's worth trying: Specify
'Browsable' and 'EditorBrowsable' attributes for the property too.
 
B

Bruce Wood

I found the problem. The property was declared as part of an interface:

public interface IHasListViewModel
{
/// <summary>
/// Gets or sets the list view model used by the control.
/// </summary>
/// <value>A list view model, or <c>null</c> if a list view model has
not yet been</value>
ListViewModel Model
{
get;
set;
}

/// <summary>
/// Occurs just before the <see cref="Model"/> of the list view
changes.
/// </summary>
event System.EventHandler ModelChanging;

/// <summary>
/// Occurs just after the <see cref="Model"/> of the list view has
changed.
/// </summary>
event System.EventHandler ModelChanged;

/// <summary>
/// Gets an indicator of whether the user is to be shown check boxes
/// in order to select multiple items that way, rather than by
multi-selection.
/// </summary>
/// <value><c>true</c> if the user is being shown check boxes as a
means of
/// selecting multiple items; <c>false</c> if not.</value>
bool CheckBoxes
{
get;
}
}

However, if you'll recall, the property as implemented in the
ListViewForSelfKeyedCollection class has both a getter and a setter,
not just a getter as declared in the interface.

This implementation is correct: the interface requires only a getter,
and some implementing user controls don't have a setter. C# allows
this, too: a class can implement _more than_ specified by the
interface, but never less.

So, the whole thing compiles fine.

However, the Designer hates this. It must be looking at something to do
with the interface, because if I _set_ the property in the Designer, it
drops the user control from the design surface.

So, I added a setter to the interface declaration, and added no-op
setters to the user controls that shouldn't have them, and lo! The
Designer works now.

That there's a bug, folks!
 

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