动态选项对话框(使用反射) [英] Dynamic options dialog (using reflection)

查看:162
本文介绍了动态选项对话框(使用反射)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道一个好的成分(C#的WinForms),它允许创建一个选项(设置)的形式,给出了一堆属性自定义类?我不是在寻找的东西闪亮的事,但仅仅比属性网格更好。我可以很容易地照顾视觉的一部分,但我根本就不想浪费时间做反思添加和绑定控件如果它已经存在。

Does anyone know of a good component (C# WinForms) which would allow creating an options (settings) form, given a custom class with a bunch of properties? I am not looking for something shiny, but something merely better than a property grid. I can easily take care of the visual part, but I simply don't want to lose time doing reflection to add and bind controls if it already exists.

我是pretty相信我已经看到了Visual Studio的选项状在什么地方,这是动态创建的(与连接到类的属性一些属性,允许分组和其他信息)。

I am pretty sure I've seen a Visual Studio options-like form somewhere before, which was created dynamically (with some attributes attached to the properties of the class, to allow grouping and additional info).

例如,我可能有一个选项类别:

For example, I might have an options class:

public class Options : SerializableOptions<Options>
{
     [Category("General")]
     [Name("User name")]
     [Description("Some text")]
     public string Username { get; set; }

     [Category("General")]
     [Name("Log in automatically")]
     public bool LogInAutomatically { get; set; }

     [Category("Advanced")]
     // ConnectionType is enum
     public ConnectionType ConnectionType { get; set; }

     // ...
}

传递到这个表格之后,它会造成两个面板(常规和高级),与第二面板上的CheckBox和第一面板上的文本框,和一个组合框(与所有可用的枚举)。

After passing it to this form, it would create two panels ("General" and "Advanced"), with a CheckBox and a TextBox on the first panel, and one ComboBox (with all available enums) on the second panel.

如果没有这样的控制,做你们用什么?手动添加,填充,格式和控件绑定为每个选项?

If there isn't such a control, what do you guys use? Manually add, populate, format and bind controls for each option?

推荐答案

我不知道,让你这样做的任何控制,但它不是很难做到的自己。最简单的方法是创建对话框窗口,它作为基类的选项面板,一个(或多个)属性来控制的名称和分组信息的用户控制和接口(其中用户控件实现)

I'm not aware of any controls that allow you to do this, but it isn't difficult to do yourself. The easiest way is to create the dialog shell, a user control which acts as the base class for the options "panels", one (or more) attribute to control the name and grouping information, and an interface (which the user control implements).

每个自定义选项面板从用户控件派生并重写某种初始化()保存()方法(由用户控制提供)。它还提供您的属性(或属性)确定名称/分组信息。

Each of your custom options panels derives from the user control and overrides some sort of Initialize() and Save() method (provided by the user control). It also provides your attribute (or attributes) that determine the name/grouping information.

在该对话框壳,反射性地检查从组装寻找实现你的接口类型的所有公共类型(或所有加载的程序集)。当你发现一个类型,获得的属性来决定在哪里把它放在您的分组(这里最简单的办法是使用一个树视图),请拨打 Activator.CreateInstance 以创建一个用户控件的实例并将其存储在标签属性。当用户点击在分组中的条目(树节点),获得标签,并设置包含在<$ C $用户控件对象面板C>标签属性。最后,当用户点击OK对话框中,遍历树节点,得到标签属性和调用保存方法。

In the dialog shell, reflectively inspect all public types from your assembly (or all loaded assemblies) looking for types that implement your interface. As you find a type, get the attributes to determine where to place it in your grouping (easiest thing here is to use a tree view), call Activator.CreateInstance to create an instance of the user control and store it in the Tag property. When the user clicks on an entry in the grouping (a tree node), get the Tag and set the panel which contains the user control to the object in the Tag property. Finally, when the user clicks "OK" on the dialog, loop through the tree nodes, get the Tag property and call the Save method.

更新:
另一种选择是使用属性网格控制。它没有一个prettyUI的外观,但它是非常实用,已经被一个类别属性支持分组,并允许具有很大的灵活性。你可以用一个属性网格显示所有的选项去,或者与由主要功能(插件,能力等)的群体,很可能是基于类型树视图混合的方式去。当用户点击该节点,赋予属性网格对象实例。这种方法唯一的缺点是,当更改属性网格值进行他们是活在底层的属性立即更改,这意味着不存在取消短期保存每个值的副本的概念,可以改变和执行某种类型的重置自己。

Update: Another option would be to use a property grid control. It doesn't have a "pretty" UI look to it, but it is very functional, already supports grouping by a category attribute, and allows a great deal of flexibility. You could go with a single property grid that shows all of the options, or go with a "hybrid" approach with a tree view that groups by major functions (plugin, capability, etc.), probably based on the type. When the user clicks that node, give the property grid the object instance. The only drawback to this approach is that when changes are made to the property grid values they are "live" in that the underlying property is immediately changed, which means there is no concept of "Cancel" short of saving a copy of each value that could change and performing some type of "reset" yourself.

这篇关于动态选项对话框(使用反射)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆