C# 属性网格:如何在网格右侧设置每个属性的显示名称标签?
我已经实现了一个自定义属性网格,我想知道是否可以从左到右更改每个属性标签的位置 我的自定义属性网格显示的属性如图所示 我想把它改成这样: 这是自定义属性网格的主代码C# 属性网格:如何在网格右侧设置每个属性的显示名称标签?,c#,.net,winforms,propertygrid,customproperty,C#,.net,Winforms,Propertygrid,Customproperty,我已经实现了一个自定义属性网格,我想知道是否可以从左到右更改每个属性标签的位置 我的自定义属性网格显示的属性如图所示 我想把它改成这样: 这是自定义属性网格的主代码 public class CustomPropertyGrid : PropertyGrid { private System.ComponentModel.Container components = null; private ReperesentAttr representAttr; privat
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;
}
}
公共类CustomPropertyGrid:PropertyGrid
{
private System.ComponentModel.Container components=null;
私人再培训代表;
私人myTab选项卡;
公共CustomPropertyGrid()
{
this.representAttr=新的representAttr(“,”);
tab.SetRepresentAttr=此.representAttr;
初始化组件();
this.PropertySort=PropertySort.alphastical;
this.rightoleft=rightoleft.Yes;
//这个。
}
公共重新设置TTR SetRepresentAttr
{
设置
{
representAttr=值;
tab.SetRepresentAttr=此.representAttr;
}
}
受保护的覆盖无效处置(布尔处置)
{
如果(处置)
{
if(组件!=null)
{
组件。Dispose();
}
}
基地。处置(处置);
}
#区域Codice-generato da Progettazione-component
///
///我必须为项目提供必要的支持。不可修改
///我是科迪斯的编辑。
///
私有void InitializeComponent()
{
//
//用户控制1
//
this.Name=“myPropertyGrid”;
}
#端区
受保护的重写属性选项卡CreatePropertyTab(类型tabType)
{
tab=新的myTab(representAttr);
返回选项卡;
}
}
公共类myTab:PropertyTab
{
私人再培训代表;
公共myTab(representAttr representAttr)
{
this.representAttr=representAttr;
}
公共重新设置TTR SetRepresentAttr
{
设置
{
representAttr=值;
}
}
//获取所选组件的属性
公共重写System.ComponentModel.PropertyDescriptorCollection GetProperties(对象组件,System.Attribute[]属性)
{
PropertyDescriptorCollection属性;
如果(属性!=null)
properties=TypeDescriptor.GetProperties(组件、属性);
其他的
properties=TypeDescriptor.GetProperties(组件);
//Componet必须实现ICUSTOMCLASS接口。
if(组件为ICustomClass)
{
ICustomClass bclass=(ICustomClass)组件;
//基于“模型”的PublicProperties属性的新属性数组
PropertyDescriptor[]arrProp=新的PropertyDescriptor[bclass.PublicProperties.Count];
对于(int i=0;i
将此添加到您的CustomPropertyGrid,这可能会让您开始:
const int WS_EX_LAYOUTRTL = 0x400000;
private bool _RTL = false;
[Description("Change to the right-to-left layout."), DefaultValue(false),
Localizable(true), Category("Appearance"), Browsable(true)]
public bool Mirrored
{
get
{
return _RTL;
}
set
{
if (_RTL != value)
_RTL = value;
base.OnRightToLeftChanged(EventArgs.Empty);
}
}
protected override CreateParams CreateParams
{
get
{
CreateParams CP;
CP = base.CreateParams;
if (this.Mirrored)
CP.ExStyle = CP.ExStyle | WS_EX_LAYOUTRTL;
return CP;
}
}
我认为你做不到这一点。winforms属性网格的内部网格不可配置。随意猜测,但DataGridViewCellStyle.Alignment属性可能有帮助吗?示例:this.dataGridView1.Columns[“CustomerName”].DefaultCellStyle.Alignment=D