Code-Generating a Winforms form from a C# POCO Class

巧了我就是萌 提交于 2019-12-09 12:57:34

问题


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?

Examples:

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

where Color is an enum of the form

public enum Color
{
    Red,
    Green,
    Blue
}

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

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. 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.

回答1:


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.

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.

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



来源:https://stackoverflow.com/questions/8131821/code-generating-a-winforms-form-from-a-c-sharp-poco-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!