从C#POCO类代码生成Winforms表单 [英] Code-Generating a Winforms form from a C# POCO Class

查看:108
本文介绍了从C#POCO类代码生成Winforms表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否已经在某个地方写出了一些开放源代码,可以接受具有属性和验证属性的类,并输出带有与那些属性相对应的控件的Windows窗体?

Is there some open source code already written out there somewhere that will accept a class with properties and validation attributes, and output a Windows form with controls that correspond to those properties?

示例:

public bool IsRed { get; set; }

产生一个带有红色的复选框?

produces a checkbox with an Is Red? label.

public int NumberOfDays { get; set; }

会生成一个带有标签的文本框,该标签称为天数

produces a text box with a label called Number of Days and restricts input to numeric characters only.

[Required]
public Color Color { get; set; }

其中Color是以下形式的枚举

where Color is an enum of the form

public enum Color
{
    Red,
    Green,
    Blue
}

生成一个组合框,列表中填充了 Red 绿色蓝色,并使其成为必需。

produces a combo box with the list populated with Red Green and Blue, and makes it required.

理想情况下,生成的代码包括一个接受我的类实例并用实例属性的值预填充winform中控件的方法。相应地,另一种方法将控件中的现有值保存到我的类的实例中。

Ideally, the generated code includes a method that accepts an instance of my class and prepopulates the controls in the winform with the values in the properties of my instance. Correspondingly, another method saves the existing values in the controls to an instance of my class.

是否有类似的东西可用?

Is there something like that available?

请注意:我不是 在寻找:

Please note: I am not looking for:


  1. ORM或DAL

  2. 像CodeSmith这样的通用工具,除非它是免费的和开源的

  3. WPF或ASP.NET代码,它必须是Winforms

  4. NetTiers或任何类似类型的完整应用程序框架,除非我可以分析出其中的一小部分专门用于这种情况。

  1. An ORM or DAL
  2. A generalized tool like CodeSmith, unless it's free and open-source
  3. WPF or ASP.NET code, it needs to be Winforms
  4. NetTiers or any similar type of complete application framework, unless I can parse out the small part of it that applies specifically to this scenario.


推荐答案

提取 PropertyGrid 到您自己的托管控件中,但这仅能满足您的一半要求。至少请阅读 TypeConverter TypeDescriptor PropertyDescriptor

It is 'quite easy' to extract the functionality of the PropertyGrid into your own hosted controls, but that would only cover half your requirement. At a minimum, read up on TypeConverter, TypeDescriptor and PropertyDescriptor.

也可以看看: codeproject.com/KB/custom-controls/xacc_propertygrid.aspx ,这基本上将 PropertyGrid 的功能公开给了网页,但是会给你一些想法。

Also have a look at: codeproject.com/KB/custom-controls/xacc_propertygrid.aspx, this basically exposes the PropertyGrid's functionality to a web page, but it will give you some ideas.

我知道这不是一个完整的答案,但是应该指出正确的方向来编写/编写这样的工具。

I know this is not a complete answer, but should point one in the right direction to write/author such a tool.

如果为WinForms提供了这样的脚手架工具,那就太好了:)

It would be nice if such a scaffolding tool existed for WinForms :)

这篇关于从C#POCO类代码生成Winforms表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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