简体   繁体   中英

Generating TextBoxes to fill in a class

I have several classes that need to have their properties filled in. For example:

class RecordType1
{
    public DateTime Date { get; set; }
    public string Name { get; set; }
    public string Value { get; set;}
}

class RecordType2
{
    public DateTime Date { get; set; }
    public string Name { get; set; }
    public string Location { get; set; }
    public float Temperature { get; set; }
}

For RecordType1 I would like to generate a DatePicker to fill in Date and two TextBox to fill in both the Name and the Value . Likewise for RecordType2 I would like a DatePicker and three TextBox for Name, Location , and Temperature`.

I could create a file containing a separate UserControl per class that has the required TextBlocks and TextBoxes but it seems as if there could be a better way to generate a form for the user to fill in.

Is this kind of thing possible?

A quick-and-dirty way to do this in WinForms is to use a PropertyGrid control. I'm not sure if there is an equivalent in WPF, but you could host the PropertyGrid control using a WindowsFormsHost .

It is possible to do that via reflection. You start by getting the type:

Type typeinfo = typeof(RecordType1);

you can then iterate through the attributes.

If you don't want to do all this yourself you can use exceeds propertygrid.

<UserControl x:Class="Interstone.Bestelbonnen.Views.DrawingPropertiesControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:vm="clr-namespace:Interstone.Bestelbonnen.ViewModels"
         xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<xctk:PropertyGrid SelectedObject="{Binding}" AutoGenerateProperties="False" IsCategorized="False">
</xctk:PropertyGrid>           

more info on the propertygrid : http://wpftoolkit.codeplex.com/wikipage?title=PropertyGrid

I did not elaborate too much on the reflection part, I am not sure which direction you want to go.

Its certainly possible to do what you are asking, even at runtime. You would use reflection to get the various fields and then generate controls to match the field types.

Now, this isn't a very practical approach, especially considering the high likelihood that such a generated form's layout is likely to be terrible. The normal approach is to just create the forms/user controls manually.

Since you are in WPF, a good approach is to use Data Templates instead of user controls, as you can easily use them in ItemsControl s and ContentControl s

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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