Reputation: 65
I'm creating a program where the user is creating a list of actions. The possible actions all have multiple properties that need to be specified. I'd like to display those properties in a list box, similar to how Visual Studio displays object properties in design mode. Strings get textboxes, bools get checkboxes, etc. I've figured out how to display the object's members in the list, but I'm struggling to figure out how to create callbacks for each control. Heres the basic structure:
void createPropertyList(object move)
{
Type type = move.GetType();
FieldInfo[] fields = type.GetFields();
propertyListBox.Items.Clear();
foreach (var field in fields)
{
string name = field.Name;
object temp = field.GetValue(move);
if (temp is double)
{
double value = (double)temp;
Label l = new Label();
l.Content = name;
TextBox t = new TextBox();
t.Text = value.ToString("0.0000");
// put it in a grid, format it, blah blah blah
propertyListBox.Items.Add(grid);
}
// reflect on other types
}
}
I assume there's going to be a lambda involved, but how do I revert the FieldInfo array to actually reference those members so I can put the user's input back into the object?
Upvotes: 0
Views: 793
Reputation: 464
You already have control for that: http://msdn.microsoft.com/en-us/library/aa302326.aspx
Upvotes: 0
Reputation: 3261
You need to create state change event handlers for all type of controls.
For example TextBox
...
TextBox t = new TextBox();
t.Tag=name;
t.KeyUp+=t_KeyUp;
where
private void t_KeyUp(object sender, KeyEventArgs e)
{
TextBox source = (sender as TextBox);
MessageBox.Show("Field "+source.Tag + " is changed. New value is "+source.Text);
}
Upvotes: 0
Reputation: 2646
You might looking into binding the various controls with a Binding and DependencyPropertys. Its a more WPF way of doing this.
Upvotes: 0
Reputation: 65
Sorry, I should have looked in FieldInfo. GetValue has a corresponding SetValue. Works like a charm.
Upvotes: 1