Dear Team,
I have a quite basic question about how to set the default attribute values for all of my controls across the whole application.
For example all my DGVs should disallow resizing of rows, disallow multiselect, etc.
My naive approach is to derive the a custom control (e.g. DataGridViewExt) with pre-set attributes and use it across the application.
Programmatically this works quite well.
The problem is that in the VS designer properties view of the control instance the property values still appear in bold to indicate that the set value differs from the property decoration (e.g. [DefaultValue(false)] public property AllowUserToResizeRows …)
Is there a right way to re-decorate the properties, so they do not appear bold in VS designer properties list?
This would be really helpful, as this will show me what attributes are changed in the current instance only.
I will try a somewhat cumbersome approach to shadow the properties, as suggested in this SO thread
Thanks,
Kizaemon
Hi,
Thanks for your post. I tried as below and it looks all right.
Snippet
using System.ComponentModel; namespace UIExtensions { public class DataGridViewExt : Wisej.Web.DataGridView { public DataGridViewExt() { base.ShowColumnVisibilityMenu = false; base.AllowUserToResizeRows = false; base.AutoGenerateColumns = false; } [DefaultValue(false)] public new bool ShowColumnVisibilityMenu { set { base.ShowColumnVisibilityMenu = value; Invalidate(); } get { return base.ShowColumnVisibilityMenu; } } [DefaultValue(false)] public new bool AllowUserToResizeRows { set { base.AllowUserToResizeRows = value; Invalidate(); } get { return base.AllowUserToResizeRows; } } [DefaultValue(false)] public new bool AutoGenerateColumns { set { base.AutoGenerateColumns = value; Invalidate(); } get { return base.AutoGenerateColumns; } } } }
A bit more in depth about properties and Visual Studio designer serialization:
private bool ShouldSerializeShowHeader() { return this._showHeaderCount > 3; // just to make it weird. } private void ResetShowHeader() { this._showHeaderCount = 0; } * The accessor can be private, protected or public. Makes no different to VS, it will still use these methods because the pattern is in System.ComponentModel. For property extenders, simply add the Control target argument.
EOM, HTH
To share my findings.
Decorating with MetadataType attribute does not yield the expected behavior.
Shadowing properties seem to work well. I am not sure if call to Invalidate() is required.
I will go for shadowing the properties.
using System; using Wisej.Web; using System.ComponentModel; using System.ComponentModel.DataAnnotations; namespace UI.Controls { [MetadataType(typeof(DataGridViewExtMetaData))] public partial class DataGridViewExt : Wisej.Web.DataGridView { public DataGridViewExt() { this.AllowUserToResizeRows = false; this.AutoGenerateColumns = false; } // works [DefaultValue(false)] public new bool AutoGenerateColumns { set { base.AutoGenerateColumns = value; Invalidate(); } get { return base.AutoGenerateColumns; } } } public class DataGridViewExtMetaData { // does not work [DefaultValue(false)] public bool AllowUserToResizeRows { get; set; } } }
Please login first to submit.