简体   繁体   中英

Dynamic WPF form creation approaches

I'm about to create a dynamic WPF UI form from DataTable data. The screens would be fairly complex. They would contain textboxes, groupboxes, checkboxes, buttons, datagrids etc. Some of them visible, some hooked up event handlers and thing like that.

What approach of creating those dynamic screens would you choose considering performance impact and complexity requirements to write and maintain source code. Please note that this code will run a LOT so it must be efficient and blazing fast. I'm considering these options:

  1. Create Controls in code, assemble them to a tree and use the tree (Grid control) as a root element for a WPF form.

1.a) Create a XAML via XAMLReader from that screen object tree and Load it via XAMLReader inside WPF Form. Creating XAML would seem redundant to me since I can use the built tree as a Content for WPF form directly.

  1. Use XMLDocument class to create tags, obejcts and their atributes. Create a XAMLlike that and then load that XAML in WPF form.

Thanks,

Michal

Consider displaying your form in a listview and creating a DataTemplate for each of your form fields textboxes, groupboxes, checkboxes, buttons, datagrids etc.

<ListBox ItemsSource="{Binding DataFormFields}"

<DataTemplate DataType="YourTextClass">
    <StackPanel>
        <TextBlock Text="{Binding LabelText}" />
        <TextBox Text="{Binding ValueText}" />
    </StackPanel>
</DataTemplate>

<DataTemplate DataType="YourCheckClass">
    <StackPanel>
        <CheckBox Content="{Binding LabelText}" 
                  IsChecked="{Binding Checked}"/>
    </StackPanel>
</DataTemplate>

For more on DataTemplates see https://docs.microsoft.com/en-us/dotnet/framework/wpf/data/data-templating-overview

Each data template should be associated with a one of your form fields classes, using the DataType attribute, this will cause the listbox to automatically use the correct DataTemplate. For more details:

https://docs.microsoft.com/en-us/dotnet/api/system.windows.datatemplate.datatype?view=netframework-4.7.2

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