所以我能够编写一个演示项目。
你可以在Github Here!找到它
我会快速告诉你示例,有两种解决方案:
- 又快又脏:这对我来说花费的时间最多,我不得不为一些非常琐碎的事情编写大量代码。我不推荐这种方式,但这种方式不复杂且直接。
- 动态内容方式(推荐):在这里,我保持可扩展性可能有点复杂,但它更易于管理并且比第一种方式工作得更好。
由于您使用的是MVVM,因此我也使用了相同的方法。
请注意:我没有完全复制您的模型,因为我不知道在 UI 中放置什么以及所有的验证是什么,但是从示例中您将能够想办法。
我希望这会有所帮助,如有任何疑问,请随时使用 cmets 部分。
在推荐的解决方案中,您要控制的关键区域只是一个集合:
internal ObservableCollection<ComponentModel.IFormControl> FormFields => new ObservableCollection<ComponentModel.IFormControl>(new List<ComponentModel.IFormControl>()
{
new ViewModel.TextFieldInputControlViewModel(){HeaderName = "Name",PlaceholderText="e.g. John Doe",IsMandatory = true },
new ViewModel.TextFieldInputControlViewModel(){HeaderName = "Admin No" , PlaceholderText = "e.g. ABC123"},
new ViewModel.TextFieldInputControlViewModel(){HeaderName = "Phone" , PlaceholderText = "e.g. +32538349182" ,IsMandatory = true,MatchingPattern = @"^[\+]?[1-9]{1,3}\s?[0-9]{6,11}$"},
new ViewModel.TextFieldInputControlViewModel(){HeaderName = "Item Description", PlaceholderText = "e.g. My Fav Item",IsMandatory = true },
new ViewModel.TextFieldInputControlViewModel(){HeaderName = "Location Lost", PlaceholderText = "e.g. Alaska",IsMandatory = true },
new ViewModel.DateTimeFieldInputViewModel(){ HeaderName = "Date Lost",IsMandatory = true}
});
您可以从接口IFormControl 继承添加更多类型,只需在此处添加字段。在添加更多字段时就像听起来一样简单。