【发布时间】:2015-12-21 09:42:59
【问题描述】:
您好,我在用户控件中有一个函数,它将显示和创建用户控件并绑定它的值取决于属性的类型
我可以做 text 属性,但是用枚举绑定一个组合框对我不起作用这里是下面的代码
public void DisplayProperties(object obj)
{
// get Display Name
DisplayNameAttribute groupNameAttribute = Attribute.GetCustomAttribute(obj.GetType(), typeof(DisplayNameAttribute)) as DisplayNameAttribute;
if (groupNameAttribute != null)
{
propertyPanel.Controls.Add(DisplayCategory(groupNameAttribute.DisplayName));
}
else
{
propertyPanel.Controls.Add(DisplayCategory(obj.GetType().Name));
}
PropertyInfo[] propInfo = obj.GetType().GetProperties();
foreach (PropertyInfo property in propInfo)
{
BrowsableAttribute attrib = property.GetCustomAttribute(typeof(BrowsableAttribute)) as BrowsableAttribute;
if(attrib == null || attrib.Browsable == true)
{
DisplayNameAttribute propName = Attribute.GetCustomAttribute(property.GetType(), typeof(DisplayNameAttribute)) as DisplayNameAttribute;
string displayName;
if (propName != null)
{
displayName = propName.DisplayName;
}
else
{
displayName = property.Name;
}
object attrs = property.GetCustomAttributes(true);
DescriptionAttribute propertyDescription = attrs as DescriptionAttribute;
if (property.PropertyType.IsEnum)
{
ComboBoxProperty comboProperty = new ComboBoxProperty();
comboProperty.ComboDisplayName = displayName;
comboProperty.propertyComboBox.DataSource = property;
comboProperty.propertyComboBox.DataBindings.Add("SelectedValue", obj, displayName);
comboProperty.Description = "test";// propertyDescription.Description;
propertyPanel.Controls.Add(comboProperty);
}
else
{
TextBoxProperty textBoxProperty = new TextBoxProperty();
//if (property.PropertyType == typeof(string))
//{
textBoxProperty.TextDisplayName = displayName;
textBoxProperty.valueTb.DataBindings.Add(new Binding("Text", obj, displayName)); //property.GetValue(obj, null).ToString();
textBoxProperty.Description = "test";//propertyDescription.Description;
textBoxProperty.Top = _PropertyPosition;
_PropertyPosition += textBoxProperty.Height;
propertyPanel.Controls.Add(textBoxProperty);
//}
}
}
}
}
这个函数是从表单中调用的
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
TestObject textObject = new TestObject();
textObject.TestString = "testing";
textObject.TestEnum = TestObject.MyEnum.A;
propertyGrid1.DisplayProperties(textObject);
}
}
public class TestObject
{
public enum MyEnum
{
A,
B
}
public string TestString { get; set; }
//public int TestInteger { get; set; }
//public double TestDouble { get; set; }
public MyEnum TestEnum {get; set;}
}
我希望组合框包含所有枚举值和要显示的选定值。 让我知道如何解决我的问题
编辑
我做了一些阅读,我能够让枚举工作但数据绑定不起作用我确定这是因为 ValueMember 如果不是来自 DataBidning 本身这里是我更新的代码 此代码来自 UserControl 类,其中包含 2 个用户控件类,一个用于 ComboBox,第二个用于文本框
public partial class PropertyGrid : TouchObjectListBox
{
int _PropertyPosition = 0;
public PropertyGrid()
{
InitializeComponent();
}
public void DisplayProperties(object obj)
{
// get Display Name
DisplayNameAttribute groupNameAttribute = Attribute.GetCustomAttribute(obj.GetType(), typeof(DisplayNameAttribute)) as DisplayNameAttribute;
if (groupNameAttribute != null)
{
propertyPanel.Controls.Add(DisplayCategory(groupNameAttribute.DisplayName));
}
else
{
propertyPanel.Controls.Add(DisplayCategory(obj.GetType().Name));
}
PropertyInfo[] propInfo = obj.GetType().GetProperties();
foreach (PropertyInfo property in propInfo)
{
BrowsableAttribute attrib = property.GetCustomAttribute(typeof(BrowsableAttribute)) as BrowsableAttribute;
if(attrib == null || attrib.Browsable == true)
{
DisplayNameAttribute propName = Attribute.GetCustomAttribute(property.GetType(), typeof(DisplayNameAttribute)) as DisplayNameAttribute;
string displayName;
if (propName != null)
{
displayName = propName.DisplayName;
}
else
{
displayName = property.Name;
}
object attrs = property.GetCustomAttributes(true);
DescriptionAttribute propertyDescription = attrs as DescriptionAttribute;
if (property.PropertyType.IsEnum)
{
ComboBoxProperty comboProperty = new ComboBoxProperty();
comboProperty.ComboDisplayName = displayName;
comboProperty.propertyComboBox.ValueMember = displayName;
comboProperty.propertyComboBox.DataSource = Enum.GetNames(property.PropertyType);
comboProperty.propertyComboBox.DataBindings.Add(new Binding("SelectedValue",obj, displayName));
comboProperty.Description = "test";
comboProperty.Top = _PropertyPosition;
_PropertyPosition += comboProperty.Height;
propertyPanel.Controls.Add(comboProperty);
}
else
{
TextBoxProperty textBoxProperty = new TextBoxProperty();
//if (property.PropertyType == typeof(string))
//{
textBoxProperty.TextDisplayName = displayName;
textBoxProperty.valueTb.DataBindings.Add(new Binding("Text", obj, displayName));
textBoxProperty.Description = "test";// propertyDescription.Description;
textBoxProperty.Top = _PropertyPosition;
_PropertyPosition += textBoxProperty.Height;
propertyPanel.Controls.Add(textBoxProperty);
//}
}
}
}
}
private Label DisplayCategory(string groupName)
{
Label groupLabel = new Label();
groupLabel.ForeColor = SystemPens.ControlLightLight.Color;
groupLabel.BackColor = SystemPens.ControlDark.Color;
groupLabel.TextAlign = ContentAlignment.MiddleLeft;
groupLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
groupLabel.Text = groupName;
groupLabel.Width = propertyPanel.Width;
groupLabel.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
groupLabel.Top = _PropertyPosition;
_PropertyPosition += groupLabel.Height;
return groupLabel;
}
}
这个调用 UserControl 的 Form
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
List<TestObject> testObject = new List<TestObject>();
testObject.Add(new TestObject() { TestString = "testing1", TestEnum = TestObject.MyEnum.A });
testObject.Add(new TestObject() { TestString = "testing2", TestEnum = TestObject.MyEnum.B });
foreach(var obj in testObject)
{
propertyGrid1.DisplayProperties(obj);
}
}
}
public class TestObject
{
[Browsable(true)]
//[CategoryOrder("Beam Parameters", 2)]
[DisplayName("Test String")]
[Description("testString description")]
public string TestString { get; set; }
//public int TestInteger { get; set; }
//public double TestDouble { get; set; }
public enum MyEnum
{
A,
B
}
private MyEnum m_cycleMode = MyEnum.A;
[Browsable(true)]
[DisplayName("Cycle Mode")]
[Description("Specifies how this process step is processed")]
public MyEnum TestEnum { get { return m_cycleMode; } set { m_cycleMode = value; } }
}
问题是当我加载应用程序组合框时没有选择初始值,当我更改它时,它将调用属性 MyEnum 并获取结果但不显示所选值,该值再次变为空白
任何想法
【问题讨论】: