我有一个属性网格,其中将引用几个属性。我希望属性网格中的一个项目是一个按钮,或者甚至有一个省略号按钮,它将像普通win表单上的按钮一样起作用。
有没有办法做到这一点?
提前感谢您的帮助!
我有一个属性网格,其中将引用几个属性。我希望属性网格中的一个项目是一个按钮,或者甚至有一个省略号按钮,它将像普通win表单上的按钮一样起作用。
有没有办法做到这一点?
提前感谢您的帮助!
namespace MyNameSpace
{
public static class PropertyGridHelper
{
private static PropertyGrid getPropertyGridParent(object sender)
{
PropertyGrid propertyGrid = null;
ToolStripButton toolStripButton = sender as ToolStripButton;
// ToolStripButton -> ToolStrip -> PropertyGrid
if (toolStripButton != null)
{
ToolStrip toolStrip = toolStripButton.GetCurrentParent() as ToolStrip;
if (toolStrip != null)
{
propertyGrid = toolStrip.Parent as PropertyGrid;
if (propertyGrid != null)
{
propertyGrid.CollapseAllGridItems();
}
}
}
return propertyGrid;
}
private static void propertyGridCollapseAllClick(object sender, EventArgs e)
{
PropertyGrid propertyGrid = getPropertyGridParent(sender);
if (propertyGrid != null)
{
propertyGrid.CollapseAllGridItems();
}
}
private static void propertyGridExpandAllClick(object sender, EventArgs e)
{
PropertyGrid propertyGrid = getPropertyGridParent(sender);
if (propertyGrid != null)
{
propertyGrid.ExpandAllGridItems();
}
}
public static void AddCollapseExpandAllButtons(this System.Windows.Forms.PropertyGrid propertyGrid)
{
foreach (Control control in propertyGrid.Controls)
{
ToolStrip toolStrip = control as ToolStrip;
if (toolStrip != null)
{
toolStrip.Items.Add(new ToolStripButton("", Properties.Resources.CollapseAll, propertyGridCollapseAllClick));
toolStrip.Items.Add(new ToolStripButton("", Properties.Resources.ExpandAll, propertyGridExpandAllClick));
}
}
}
}
}
UITypeEditor,使用IWindowsFormsEditorService...就是这样。明白了!感谢您的指引!
为了执行它,您可以创建一个类,例如名为my_Button
的类,其中不包含任何项,并在该类的顶部定义以下属性:
[Editor(typeof(UIEditor_List), typeof(UITypeEditor)), TypeConverter(typeof(TypeConv_List)), Serializable]
public class my_Button
{
}
UIEditor_List
类用于执行该类,TypeConv_List
是它的名称,这些值可以定义如下:
public class TypeConv_List : TypeConverter
{
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
return "Click ...";
}
}
public class UIEditor_List : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
my_Button list = value as my_Button;
list = new my_Button();
return list;
}
}
foreach
属性中,定义如下。public my_Button Read_Mode { get; set; } = new my_Button();
propertygrid
中定义事件名称propertyGrid1_PropertyValueChanged
并定义您想要进行的每个更改,如下所示:private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
{
if (propertyGrid1.SelectedGridItem.Label == nameof(filr_dir.Read_Mode))
{
System.Diagnostics.Process.Start("explorer.exe", "notpad.txt");
}
}
PropertyGrid
,还是其他什么东西?(您没有指定使用哪个UI框架。) - Joe White