prevent the designer from calling a getter (VS 2008, WinForms)
I have a simple UserControl containing a ComboBox which is empty at first. The setter for that CB adds items to it and the getter returns the selected item. When adding this UC to a Form, the designer automatically calls the getter for the CB which is empty. The method to fill up the CB with items is called开发者_如何学C later. I can think of one or two ways to bypass this problem by "messing around" in the code. But before I start that I'd like to ask you if there is a way to stop the designer from calling the getter method. Maybe with a attribute similar to Browsable or Bindable? thx
It is not that clear to me what this getter might look like. However, you want to make sure that the designer doesn't serialize properties that should only ever be used at runtime. Do this with an attribute:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Browsable(false)]
public int SomeProperty {
//etc...
}
Try this:
public ListBoxItem MyProperty
{
get
{
if (this.DesignMode)
{
return new ListBoxItem("empty");
}
else
{
return comboBox1.SelectedItems[0];
}
}
}
The getter will still be called, but you can control what is returned here.
Or, I think putting the [Browsable (false)]
attribute above the getter might work, too, but I'm not sure.
精彩评论