本文介绍了propertygrid控件的selectedobject属性的功能。 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I'm creating a usercontrol and I want to replicate the SelectedObject property of the .NET PropertyGrid Control. i.e.... I want the user to be able to select a control, from a list of controls which have been placed on my UserControls' Parent form. They will be selecting the control in the propertygrid inside Visual Studio. Well, technically, they can manually assign it in code, like you can with the SelectedObject property.
我尝试过的:
更新:我在这个问题下面添加了一个解决方案,Soluton#2
What I have tried:
Update : I added a solution, down below this question, "Soluton #2
private string[] ObjectBuffer;
private string _SelectedObject = "Unknown";
[Category("Custom"), TypeConverter(typeof(SelectedObjectConverter))]
public string SelectedObject
{
get { fillit(); return _SelectedObject; }
set { _SelectedObject = value; }
}
private void fillit()
{
if (this.Parent == null) return;
List<string> listBuffer = new List<string>();
foreach(Control c in (this.Parent as Control).Controls)
{
listBuffer.Add(c.Name);
}
ObjectBuffer = listBuffer.ToArray();
listBuffer.Clear();
}
private class SelectedObjectConverter : StringConverter
{
//This works ....
//private static StandardValuesCollection SelectedObjects =
// new StandardValuesCollection(
// new string[]{"Mother", "Father", "Sister",
// "Brother", "Daughter", "Son",
// "Aunt", "Uncle", "Cousin"});
//But, how do I access the above ObjectBuffer[]
private static StandardValuesCollection SelectedObjects =
new StandardValuesCollection(ObjectBuffer);
public override bool GetStandardValuesSupported(ITypeDescriptorContext context) => true;
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) => false;
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) => SelectedObjects;
}
推荐答案
private string _SelectedObject = "Unknown";
[Category("Custom"), TypeConverter(typeof(SelectedObjectConverter))]
public string SelectedObject
{
get { fillit(); return _SelectedObject; }
set { _SelectedObject = value; }
}
private void fillit()
{
if (this.Parent == null) return;
string[] controlStrings = new string[(this.Parent as Control).Controls.Count];
for (int i = 0; i < (this.Parent as Control).Controls.Count; i++)
{
controlStrings[i] = (this.Parent as Control).Controls[i].Name;
}
SelectedObjectConverter.SelectedObjects = new SelectedObjectConverter.StandardValuesCollection(controlStrings);
}
private class SelectedObjectConverter : StringConverter
{
public static StandardValuesCollection SelectedObjects = new StandardValuesCollection(new string[0]);
public override bool GetStandardValuesSupported(ITypeDescriptorContext context) => true;
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) => false;
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) => SelectedObjects;
}
这篇关于propertygrid控件的selectedobject属性的功能。 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!