Hi, How do one set default value for objects? For simple attributes like string, one can do it like [Bindable(true), Category("Appearance"), DefaultValue("datagridstyle")] public override string CssClass { get { return base.CssClass; } set { base.CssClass = value; } } but for headerstyle-CssClass, how do one go about? I want to change the CssClass value only. If i create a TableItemStyle, where should i code this and how do i assign it to the defaultValue part? [DefaultValue("???")] public override TableItemStyle HeaderStyle { get { return base.HeaderStyle; } } Or have i make mistake... and there are better way of doing this? Thanks Joey
The DefaultValue attribute doesn't actually set the default value ... it just tells the designer what to expect as the default value ... You can set your default values like you would in any other class using initializers: private MyEnum _MyEnumVal = MyEnum.NotSet; [DefaultValue(MyEnum.NotSet)] public MyEnum MyEnumVal { get { return this._MyEnumVal; } set { this._MyEnum = value; } } In your examples, since you were wrapping access to the base class' Members ... the default values were getting set by the base class ... not your DefaultValue declaration.
As long as you have told the designer(Visual Studio) how to persist the value in code ... the initial value will get over-written by the new, different value you chose using the designer ... below is the property ShowHeader from my DataGridPager control ... below that is the tag from the page showing the non-default value I selected in Visual Studio persisted in code by a tag attribute [Category("Appearance"), Description("Show header text."), DefaultValue(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), PersistenceMode(PersistenceMode.Attribute)] public bool ShowHeader { get { if (this.ViewState["ShowHeader"] == null) { return true; //Default value } else { return (bool)this.ViewState["ShowHeader"]; } } set { this.ViewState["ShowHeader"] = value; } } <cc2:DataGridPager id=DataGridPager2 runat="server" DataGridToNavigate="dgLicensedVehicles" Font-Size="14px" ShowHeader="False"></cc2:DataGridPager>
Thanks. So is there anyway of setting the designer value and allowing changes later on? Joey [quoted text, click to view] "Matt" <mattmorg55@gmail.com> wrote in message news:1113314199.436956.303570@z14g2000cwz.googlegroups.com... > The DefaultValue attribute doesn't actually set the default value ... > it just tells the designer what to expect as the default value ... You > can set your default values like you would in any other class using > initializers: > > private MyEnum _MyEnumVal = MyEnum.NotSet; > > [DefaultValue(MyEnum.NotSet)] > public MyEnum MyEnumVal { > get { > return this._MyEnumVal; > } > set { > this._MyEnum = value; > } > } > > In your examples, since you were wrapping access to the base class' > Members ... the default values were getting set by the base class ... > not your DefaultValue declaration. >
Don't see what you're looking for? Try a search.
|