Property Attributes
-
Hi All, I want to know if it is possible to specify on your class properties the display width that a control must used when the property is assigned to a control or a column in the grid. Basically I have created a custom class. Then get data back into a List. This List gets assigned to a grid. Know I would like to assign some basic display values to the property in the class and not have to go to the grid to specify display values like width of column. Data Example: var query = from c in Clients select new Client { Name = c.Name, Email = c.Email }.ToList(); public class Client { public string Name {get; set;} public string Email {get; set;} } Any ideas will be welcome!
-
Hi All, I want to know if it is possible to specify on your class properties the display width that a control must used when the property is assigned to a control or a column in the grid. Basically I have created a custom class. Then get data back into a List. This List gets assigned to a grid. Know I would like to assign some basic display values to the property in the class and not have to go to the grid to specify display values like width of column. Data Example: var query = from c in Clients select new Client { Name = c.Name, Email = c.Email }.ToList(); public class Client { public string Name {get; set;} public string Email {get; set;} } Any ideas will be welcome!
you should assign a value to your properties.
private int myVar = 200; \[DefaultValue(200)\] //<-- This attribute will make the 200 value as default (means not in bold in property grid) public int MyProperty { get { return myVar; } set { myVar = value; } }
-
you should assign a value to your properties.
private int myVar = 200; \[DefaultValue(200)\] //<-- This attribute will make the 200 value as default (means not in bold in property grid) public int MyProperty { get { return myVar; } set { myVar = value; } }
Thanks for the reply! This does not help. I don't want to set a default value to the property. I want to set the display width of the column that is assign to my property. Or I want to set the Description that must be shown in a grid for this property via property attributes. Regards
-
Thanks for the reply! This does not help. I don't want to set a default value to the property. I want to set the display width of the column that is assign to my property. Or I want to set the Description that must be shown in a grid for this property via property attributes. Regards
I got your point. You want to reflect all your property value immediately at design time from the IDE. Start researching the Custom Control Designers : Design-time HTML for ASP.NET[^]
-
Hi All, I want to know if it is possible to specify on your class properties the display width that a control must used when the property is assigned to a control or a column in the grid. Basically I have created a custom class. Then get data back into a List. This List gets assigned to a grid. Know I would like to assign some basic display values to the property in the class and not have to go to the grid to specify display values like width of column. Data Example: var query = from c in Clients select new Client { Name = c.Name, Email = c.Email }.ToList(); public class Client { public string Name {get; set;} public string Email {get; set;} } Any ideas will be welcome!
Hi, The best solution for you depends on many things such as which controls are you using, are you building your own controls, shold the display values depend on the data etc. Few possibilities you could consider: 1. You could add properties to your class which tell the preferred size of a control that display the property. For example:
public class Client {
public string Name { get; set;}
public string Email { get; set;}
public System.Drawing.Size PreferredSizeForName {
get {
return new System.Drawing.Size(35, 13);
}
}
}After that you can bind the size of a control to that property using advanced bindings or code 2. Use custom attributes. Derive your own attribute from System.Attribute and use reflection in your code to see what is the preferred size and then adjust your own control by that value. For example:
[System.AttributeUsage(AttributeTargets.Property)]
public class PreferredSizeAttribute : System.Attribute {
private int _width;
private int _height;public PreferredSizeAttribute(int width, int height) { this.\_width = width; this.\_height = height; } public int Width { get { return this.\_width; } set { this.\_width = value; } } public int Height { get { return this.\_height; } set { this.\_height = value; } } public System.Drawing.Size Size { get { return new System.Drawing.Size(this.Width, this.Height); } }
}
and then usage:
public class Client {
private string name;
[PreferredSize(35, 13)]
public string Name {
get {
return name;
}
set {
name = value;
}
}
public string Email { get; set;}
}Refer to documentation how to check the existence of an attribute and get it's values. Hope this helps, Mika
-
Hi, The best solution for you depends on many things such as which controls are you using, are you building your own controls, shold the display values depend on the data etc. Few possibilities you could consider: 1. You could add properties to your class which tell the preferred size of a control that display the property. For example:
public class Client {
public string Name { get; set;}
public string Email { get; set;}
public System.Drawing.Size PreferredSizeForName {
get {
return new System.Drawing.Size(35, 13);
}
}
}After that you can bind the size of a control to that property using advanced bindings or code 2. Use custom attributes. Derive your own attribute from System.Attribute and use reflection in your code to see what is the preferred size and then adjust your own control by that value. For example:
[System.AttributeUsage(AttributeTargets.Property)]
public class PreferredSizeAttribute : System.Attribute {
private int _width;
private int _height;public PreferredSizeAttribute(int width, int height) { this.\_width = width; this.\_height = height; } public int Width { get { return this.\_width; } set { this.\_width = value; } } public int Height { get { return this.\_height; } set { this.\_height = value; } } public System.Drawing.Size Size { get { return new System.Drawing.Size(this.Width, this.Height); } }
}
and then usage:
public class Client {
private string name;
[PreferredSize(35, 13)]
public string Name {
get {
return name;
}
set {
name = value;
}
}
public string Email { get; set;}
}Refer to documentation how to check the existence of an attribute and get it's values. Hope this helps, Mika