Tôi đã triển khai lưới thuộc tính tùy chỉnh, tôi muốn biết liệu tôi có thể thay đổi vị trí nhãn của mỗi thuộc tính từ trái sang phải hay không.Lưới thuộc tính: Cách đặt nhãn Tên hiển thị của mỗi thuộc tính ở bên phải lưới?
Lưới thuộc tính tùy chỉnh của tôi hiển thị thuộc tính như bạn có thể thấy trong hình.
Tôi muốn thay đổi nó được như thế này:
Đây là mã chính của lưới Tuỳ chỉnh tài sản
public class CustomPropertyGrid : PropertyGrid
{
private System.ComponentModel.Container components = null;
private ReperesentAttr representAttr;
private myTab tab;
public CustomPropertyGrid()
{
this.representAttr = new ReperesentAttr("", "");
tab.SetRepresentAttr = this.representAttr;
InitializeComponent();
this.PropertySort = PropertySort.Alphabetical;
this.RightToLeft = RightToLeft.Yes;
// this.
}
public ReperesentAttr SetRepresentAttr
{
set
{
representAttr = value;
tab.SetRepresentAttr = this.representAttr;
}
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Codice generato da Progettazione componenti
/// <summary>
/// Metodo necessario per il supporto della finestra di progettazione. Non modificare
/// il contenuto del metodo con l'editor di codice.
/// </summary>
private void InitializeComponent()
{
//
// UserControl1
//
this.Name = "myPropertyGrid";
}
#endregion
protected override PropertyTab CreatePropertyTab(Type tabType)
{
tab = new myTab(representAttr);
return tab;
}
}
public class myTab : PropertyTab
{
private ReperesentAttr representAttr;
public myTab(ReperesentAttr representAttr)
{
this.representAttr = representAttr;
}
public ReperesentAttr SetRepresentAttr
{
set
{
representAttr = value;
}
}
// get the properties of the selected component
public override System.ComponentModel.PropertyDescriptorCollection GetProperties(object component, System.Attribute[] attributes)
{
PropertyDescriptorCollection properties;
if (attributes != null)
properties = TypeDescriptor.GetProperties(component, attributes);
else
properties = TypeDescriptor.GetProperties(component);
//Componet must implement the ICUSTOMCLASS interface.
if (component is ICustomClass)
{
ICustomClass bclass = (ICustomClass)component;
//The new array of properties, based on the PublicProperties properties of "model"
PropertyDescriptor[] arrProp = new PropertyDescriptor[bclass.PublicProperties.Count];
for (int i = 0; i < bclass.PublicProperties.Count; i++)
{
//Find the properties in the array of the propertis which neme is in the PubliCProperties
PropertyDescriptor prop = properties.Find(bclass.PublicProperties[i].Name, true);
//Build a new properties
arrProp[i] = TypeDescriptor.CreateProperty(prop.ComponentType, prop, new CategoryAttribute(this.representAttr.Category));
}
return new PropertyDescriptorCollection(arrProp);
}
else
{
return properties;
}
}
public override System.ComponentModel.PropertyDescriptorCollection GetProperties(object component)
{
return this.GetProperties(component, null);
}
// PropertyTab Name
public override string TabName
{
get
{
return "Properties";
}
}
//Image of the property tab (return a blank 16x16 Bitmap)
public override System.Drawing.Bitmap Bitmap
{
get
{
return new Bitmap(16, 16);
}
}
}
public class ReperesentAttr
{
string category = string.Empty;
string name = string.Empty;
public ReperesentAttr(string name, string category)
{
this.category = category;
this.name = name;
}
public string Category
{
set { category = value; }
get { return category; }
}
public string Name
{
set { name = value; }
get { return name; }
}
}
public interface ICustomClass
{
PropertyList PublicProperties
{
get;
set;
}
}
public class PropertyList : NameObjectCollectionBase
{
public void Add(Object value)
{
//The key for the object is taken from the object to insert
this.BaseAdd(((CustomProperty)value).Name, value);
}
public void Remove(String key)
{
this.BaseRemove(key);
}
public void Remove(int index)
{
this.BaseRemoveAt(index);
}
public void Clear()
{
this.BaseClear();
}
public CustomProperty this[String key]
{
get
{
return (CustomProperty)(this.BaseGet(key));
}
set
{
this.BaseSet(key, value);
}
}
public CustomProperty this[int indice]
{
get
{
return (CustomProperty)(this.BaseGet(indice));
}
set
{
this.BaseSet(indice, value);
}
}
public bool HasKey(String key)
{
foreach(String item in this.BaseGetAllKeys())
{
if(key == item)
return true;
}
return false;
}
}
Tôi không nghĩ bạn có thể làm điều này. Lưới nội bộ của lưới thuộc tính winforms không thể cấu hình được. –
Đoán hoang dã nhưng có lẽ DataGridViewCellStyle.Alignment Property có thể giúp bạn? Ví dụ: this.dataGridView1.Columns ["CustomerName"]. DefaultCellStyle .Alignment = DataGridViewContentAlignment.MiddleRight; – krzysztofla
Đó là lưới thuộc tính và khác với dataGrid. @KrzysztofLa –