简体   繁体   English

在WPF / C#中为多个对象创建Visual Studio属性窗口

[英]Creating a Visual Studio properties window for multiple object in WPF/C#

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. 我想在列表框中显示这些属性,类似于Visual Studio在设计模式下显示对象属性的方式。 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? 我假设将涉及一个lambda,但是如何还原FieldInfo数组以实际引用那些成员,以便可以将用户的输入放回该对象中?

Sorry, I should have looked in FieldInfo. 抱歉,我应该在FieldInfo中查看。 GetValue has a corresponding SetValue. GetValue具有对应的SetValue。 Works like a charm. 奇迹般有效。

You might looking into binding the various controls with a Binding and DependencyPropertys. 您可能希望通过Binding和DependencyPropertys绑定各种控件。 Its a more WPF way of doing this. 这是一种更WPF的方式。

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);
}

您已经对此具有控制权: http : //msdn.microsoft.com/zh-cn/library/aa302326.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM