sal*_*e55 24 .net c# propertygrid winforms
如何在运行时以各种方式修改propertygrid?我希望能够添加和删除属性并添加"动态类型",我的意思是使用TypeConverter导致在propertygrid中生成运行时生成的下拉列表.
我实际上已经能够做这两件事(添加/删除属性和添加动态类型)但不能同时单独使用.
为了实现在运行时添加和删除属性的支持,我使用了这个代码项目文章并稍微修改了代码以支持不同的类型(不仅仅是字符串).
private System.Windows.Forms.PropertyGrid propertyGrid1;
private CustomClass myProperties = new CustomClass();
public Form1()
{
InitializeComponent();
myProperties.Add(new CustomProperty("Name", "Sven", typeof(string), false, true));
myProperties.Add(new CustomProperty("MyBool", "True", typeof(bool), false, true));
myProperties.Add(new CustomProperty("CaptionPosition", "Top", typeof(CaptionPosition), false, true));
myProperties.Add(new CustomProperty("Custom", "", typeof(StatesList), false, true)); //<-- doesn't work
}
/// <summary>
/// CustomClass (Which is binding to property grid)
/// </summary>
public class CustomClass: CollectionBase,ICustomTypeDescriptor
{
/// <summary>
/// Add CustomProperty to Collectionbase List
/// </summary>
/// <param name="Value"></param>
public void Add(CustomProperty Value)
{
base.List.Add(Value);
}
/// <summary>
/// Remove item from List
/// </summary>
/// <param name="Name"></param>
public void Remove(string Name)
{
foreach(CustomProperty prop in base.List)
{
if(prop.Name == Name)
{
base.List.Remove(prop);
return;
}
}
}
Run Code Online (Sandbox Code Playgroud)
等等...
public enum CaptionPosition
{
Top,
Left
}
Run Code Online (Sandbox Code Playgroud)
我的完整解决方案可以在这里下载.
当我添加字符串,bool或枚举时,它工作正常,但当我尝试添加像StatesList这样的"动态类型"时它不起作用.有谁知道为什么,可以帮助我解决它?
public class StatesList : System.ComponentModel.StringConverter
{
private string[] _States = { "Alabama", "Alaska", "Arizona", "Arkansas" };
public override System.ComponentModel.TypeConverter.StandardValuesCollection
GetStandardValues(ITypeDescriptorContext context)
{
return new StandardValuesCollection(_States);
}
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
当您不尝试在运行时添加属性时,使用TypeConverter的方法可以正常工作,例如,此代码可以正常工作,但我希望能够同时执行这两个操作.
请看看我的项目.谢谢!
啊,当然!
myProperties.Add(new CustomProperty("Custom", "", typeof(States), false, true));
[TypeConverter(typeof(StatesList))]
public class States
{
}
Run Code Online (Sandbox Code Playgroud)
像魅力一样,谢谢!
我已经更新了我的项目,希望它对其他人有帮助,可以在这里找到.
这个问题和答案对我很有用。但是,我需要通过允许运行时生成的下拉列表值来进一步扩展。我想我会发布一些关于它需要什么的示例代码,以防有人觉得它有用。
首先,我向 CustomProperty 构造函数添加了一个 options 参数并添加了一个 Options 属性:
private List<string> lOptions;
public CustomProperty(string sName, object value, Type tType, bool bReadOnly, bool bVisible, List<string> lOptions)
{
this.lOptions = lOptions;
}
public List<string> Options
{
get { return lOptions; }
}
Run Code Online (Sandbox Code Playgroud)
其次,我向 CustomPropertyDescriptor 类添加了一个 Options 属性:
public List<string> Options
{
get
{
return m_Property.Options;
}
}
Run Code Online (Sandbox Code Playgroud)
第三,我必须修改动态类型类(即 StatesList)中的 GetStandardValues 方法,以利用 CustomPropertyDescriptor 对象上的新选项属性:
public override StandardValuesCollection
GetStandardValues(ITypeDescriptorContext context)
{
CustomPropertyDescriptor descriptor = (CustomPropertyDescriptor)context.PropertyDescriptor;
return new StandardValuesCollection(descriptor.Options);
}
Run Code Online (Sandbox Code Playgroud)
最后,在创建新的 CustomProperty 对象时,我必须传入我的选项列表:
List<string> optionsList = new List<string>(new string[] { "test1", "test2", "test3" });
CustomProperty myProperty = new CustomProperty(attr.Name, attr.Value, valueType, false, true, optionsList);
Run Code Online (Sandbox Code Playgroud)
代替我在此示例中传入的静态列表,您可以按照您喜欢的任何方式为下拉列表生成选项列表,让您完全控制可用选项。