如何在 WPF 应用程序中构建动态数据输入表单? [英] How to build dynamic data entry forms in a WPF application?

查看:53
本文介绍了如何在 WPF 应用程序中构建动态数据输入表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在计划一个 WPF 应用程序

I'm planning a WPF application which will

  • 能够创建动态数据输入表单(意味着表单从数据库中的数据中获取要显示的字段、它们的顺序等,而不是从 XAML 中获取)
  • 尽可能使用 MVVM 模式
  • be able to create dynamic data entry forms (meaning the form gets the fields to display, their order, etc. from data in the database, not from the XAML)
  • use the MVVM pattern if possible

这是我计划的方法:在客户数据条目视图中,我将设置数据上下文:

Here is how I plan to go about it: in a Customer Data Entry View I would set the data context:

<UserControl.DataContext>
    <vm:DynamicFormViewModel/>
</UserControl.DataContext>

然后在我的 XAML 中包含一个元素作为表单的占位符:

and then include one element in my XAML as a placeholder for the form:

<UserControl.Content>
    <view:DynamicFormView x:Name="CustomerEntry"/>
</UserControl.Content>

然后在我的 ModelView 中,我不想有静态属性,但我想以这种方式将 XAML 构建为 ASP.NET 中的一个构建的 HTML 控件:

then in my ModelView I want to not have static properties but I want to build the XAML as one built HTML controls back in ASP.NET, in this fashion:

View view = new View();
view.Children.Add(...)

并以这种方式基于 ViewModel 从模型获取的数据(名字、姓氏)和元数据(字段标签、字段名称、字段帮助文本、字段显示顺序等)的集合构建网格.

and in this way build the Grid based on the collection of data (firstname, lastname) and meta data (field label, field name, field helptext, field display order, etc.) which the ViewModel gets from the Model.

  • 是否有人构建了可以以这种方式创建动态表单的 WPF 应用程序?
  • 你使用过 MVVM 模式吗?
  • 是否可以以这种方式使用 MVVM 模式,或者 MVVM 模式是否预先假定了 View Model 中的静态字段直接绑定到 View 中的静态元素?

推荐答案

您必须为各种字段数据类型编写数据模板,以便 WPF 将根据数据类型选择如何显示数据.这种格式的东西:

You will have to write data templates for your various field data types so that WPF will chose how to display your data depending on its type. something of this format:

注意:这不是 WPF 只是伪代码

<DataTemplate DataType="{x:Type DateTime}">
  <DatePicker Value="{Binding}"/>
</DataTemplate>  
<DataTemplate DataType="{x:Type String}">
  <TextBox Text="{Binding}"/>
</DataTemplate>

它不必是原始类型.它可以是 EmailDateApproved 甚至 Url 类类型.例如

It doesn't have to be a primitive type. It can be an Email, DateApproved or even a Url class type. e.g.

class Customer  
{
   public Email Email{get;set;}
   public DateTime DateApproved{get;set;}
   public URI Url{get;set;}
}

public class Email 
{
   public string Type{get;set;}
   public string Value{get;set;} 
} 

...等等...

更新

在 MSDN 上查看此 WPF 动态 UI 示例:Dynamic Data使用 WPF 和 LINQ 输入

Check out this WPF Dynamic UI example on MSDN: Dynamic Data Entry with WPF and LINQ

这篇关于如何在 WPF 应用程序中构建动态数据输入表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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