110,534
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 页面类型
/// </summary>
[Editor(typeof(PageTypeEditor), typeof(UITypeEditor))]
public string PageType
{
get;
set;
}
internal class PageTypeEditor:UITypeEditor
{
public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
{
//return base.EditValue(context, provider, value);
var edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (edSvc != null)
{
PageTypeDropdown dropList = new PageTypeDropdown();
var str = string.Empty;
if (value != null)
{
str = value.ToString();
}
if(!string.IsNullOrEmpty(str) && !dropList.ContainItemText(str))
{
dropList.Items.Insert(0,str);
}
dropList.Text = str;
dropList.EditorService = edSvc;
edSvc.DropDownControl(dropList);
return dropList.Text;
}
return value;
}
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
//return base.GetEditStyle(context);
return UITypeEditorEditStyle.DropDown;
}
}
internal class PageTypeDropdown : ListView
{
public PageTypeDropdown()
{
this.View = System.Windows.Forms.View.List;
this.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.MultiSelect = false;
this.FullRowSelect = true;
this.Items.Add("内容页");
this.Items.Add("封面");
}
public override string Text
{
get
{
return GetSeletedText();
}
set
{
SetSelectItem(value);
}
}
public IWindowsFormsEditorService EditorService
{
get;
set;
}
public bool ContainItemText(string text)
{
foreach (ListViewItem item in Items)
{
if (item.Text == text)
{
return true;
}
}
return false;
}
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
CloseDropDown();
}
private void CloseDropDown()
{
if (EditorService != null)
{
EditorService.CloseDropDown();
}
}
private string GetSeletedText()
{
if (this.SelectedItems.Count > 0)
{
var item = SelectedItems[0];
return item.Text;
}
return string.Empty;
}
private void SetSelectItem(string text)
{
foreach (ListViewItem item in Items)
{
if (item.Text == text)
{
item.Selected = true;
break;
}
}
}
}
Type type = typeof(System.ComponentModel.BrowsableAttribute);
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(obj);
AttributeCollection attrs = props[propertyName].Attributes;
FieldInfo fld = type.GetField("browsable", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.CreateInstance);
fld.SetValue(attrs[type], readOnly);
[Browsable(true), DefaultValue(TTT.t1)]
public TTT Test
{
get;
set;
}
public enum TTT
{
t1,
t2,
t3
}
把属性又int改枚举是可以的
Type type = typeof(System.ComponentModel.BrowsableAttribute);
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(obj);
AttributeCollection attrs = props[propertyName].Attributes;
FieldInfo fld = type.GetField("browsable", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.CreateInstance);
fld.SetValue(attrs[type], readOnly);
[/quote]
可以做到,利用反射标签属性BrowsableAttribute,改变其值
Type type = typeof(System.ComponentModel.BrowsableAttribute);
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(obj);
AttributeCollection attrs = props[propertyName].Attributes;
FieldInfo fld = type.GetField("browsable", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.CreateInstance);
fld.SetValue(attrs[type], readOnly);
[/quote]
这种方式不行呀,这么设置完之后,PropertyGrid没有实时变化,而把PropertyGrid关了之后再打开PropertyGrid,所有的属性标签都没了,感觉这种方式是永久性的设置属性值