在 C# 中使用 CollectionEditor 更改项目的名称
Changing the Name of item using a CollectionEditor in C#
对于设计时支持,我正在实施 CollectionEditor
如下:
class CustomCollectionEditor : CollectionEditor
{
...
protected override CollectionForm CreateCollectionForm()
{
...
var propertyGrid = form.Controls.Find(
PropertyGridComponentName, true).First() as PropertyGrid;
...
}
...
}
通过以下方式附加到自定义控件:
[Editor(typeof(CustomCollectionEditor), typeof(UITypeEditor))]
class CustomControl : Control
{
...
}
通过覆盖 CollectionEditor
class 的 CreateCollectionForm()
方法,我可以访问 PropertyGrid
并且能够通过访问适当的方法来更改所选项目的属性PropertyDescriptor
个实例。
但是,设置 Name
属性 似乎不会更新集合编辑器中的实例名称。
如果我尝试以下操作:
var descriptor = TypeDescriptor.GetProperties(propertyGrid.SelectedObject)["Name"];
descriptor.SetValue(propertyGrid.SelectedObject, "instanceName");
该语句似乎在执行,但 属性 网格中所选项目的 名称 没有改变。
因此,我的问题是,如果可能的话,我如何以编程方式更新所选项目的 Name
属性?
为清楚起见,我尝试从代码更新在 属性 网格的 Name 字段中键入的内容,如下所示:
所有其他属性(例如、"ForeColor"、"Text"、等)可以使用上述方法可以毫无问题地进行更改;只是 Name
属性 造成了困难。我认为这是因为 Name
不是 "real" 属性。
如有任何建议,我们将不胜感激。
对于设计时支持,我正在实施 CollectionEditor
如下:
class CustomCollectionEditor : CollectionEditor
{
...
protected override CollectionForm CreateCollectionForm()
{
...
var propertyGrid = form.Controls.Find(
PropertyGridComponentName, true).First() as PropertyGrid;
...
}
...
}
通过以下方式附加到自定义控件:
[Editor(typeof(CustomCollectionEditor), typeof(UITypeEditor))]
class CustomControl : Control
{
...
}
通过覆盖 CollectionEditor
class 的 CreateCollectionForm()
方法,我可以访问 PropertyGrid
并且能够通过访问适当的方法来更改所选项目的属性PropertyDescriptor
个实例。
但是,设置 Name
属性 似乎不会更新集合编辑器中的实例名称。
如果我尝试以下操作:
var descriptor = TypeDescriptor.GetProperties(propertyGrid.SelectedObject)["Name"];
descriptor.SetValue(propertyGrid.SelectedObject, "instanceName");
该语句似乎在执行,但 属性 网格中所选项目的 名称 没有改变。
因此,我的问题是,如果可能的话,我如何以编程方式更新所选项目的 Name
属性?
为清楚起见,我尝试从代码更新在 属性 网格的 Name 字段中键入的内容,如下所示:
所有其他属性(例如、"ForeColor"、"Text"、等)可以使用上述方法可以毫无问题地进行更改;只是 Name
属性 造成了困难。我认为这是因为 Name
不是 "real" 属性。
如有任何建议,我们将不胜感激。