小弟在使用PropertyGrid时遇到了一个难题,请各位帮帮忙。问题如下:
假设有一个程序如此
该段代码大致如下:
namespace PropertyTest {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
propertyGrid1.SelectedObject = new Fun();
}
} public class Fun {
private List<TestArray> test=new List<TestArray>(); public string Name { get; set; }
public int Num { get; set; }
public List<TestArray> Test { get; set; } }
}
点击Test后,会进入如下窗体:
其中Text绑定了一个自定义窗体,TestArray代码如下:
public class TestArray { public string TempName { get; set; }
public int TempNum { get; set; } [Editor(typeof(TextEditor), typeof(System.Drawing.Design.UITypeEditor))]
public string Text{get;set;}
} Editor的代码如下:
namespace PropertyTest {
public class TextEditor :UITypeEditor {
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) {
return UITypeEditorEditStyle.Modal;
} public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) {
IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (edSvc != null) {
TextInput t = new TextInput(edSvc);
edSvc.ShowDialog(t);
return t.S;
}
return value;
}
}
}
我的问题是该如何在Editor的EditValue中获取最顶级窗口的Name和Num的值,请各位帮帮忙。
假设有一个程序如此
该段代码大致如下:
namespace PropertyTest {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
propertyGrid1.SelectedObject = new Fun();
}
} public class Fun {
private List<TestArray> test=new List<TestArray>(); public string Name { get; set; }
public int Num { get; set; }
public List<TestArray> Test { get; set; } }
}
点击Test后,会进入如下窗体:
其中Text绑定了一个自定义窗体,TestArray代码如下:
public class TestArray { public string TempName { get; set; }
public int TempNum { get; set; } [Editor(typeof(TextEditor), typeof(System.Drawing.Design.UITypeEditor))]
public string Text{get;set;}
} Editor的代码如下:
namespace PropertyTest {
public class TextEditor :UITypeEditor {
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) {
return UITypeEditorEditStyle.Modal;
} public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) {
IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (edSvc != null) {
TextInput t = new TextInput(edSvc);
edSvc.ShowDialog(t);
return t.S;
}
return value;
}
}
}
我的问题是该如何在Editor的EditValue中获取最顶级窗口的Name和Num的值,请各位帮帮忙。
{
TestArray ta = context.Instance as TestArray;
...
}
TestArray是可以取到,但我要获得的是第一个窗体的Name,也就是Fun类中的属性,这时候Instance就取不到了
public static Fun EdittingObject; // might use a static helper
public Form1()
{
InitializeComponent();
this.propertyGrid1.SelectedObjectsChanged += delegate
{
Form1.EdittingObject = this.propertyGrid1.SelectedObject as Fun;
};
}