[英]How to build a WPF control containing other controls based on a bound list
I have got a ViewModel that looks like我有一个看起来像的 ViewModel
public class MyViewModel
{
public ObservableCollection<UserDefinedFieldBase> CustomFields { get; }
}
public class UserDefinedFieldBase
{
public Point CustomLocation { ... }
}
public class CustomTextField : UserDefinedFieldBase
{
public string Text { ... }
}
So every possible type of field has a own type and includes a location (which is customizable by the user, so it's part of the viewModel, not the view.因此,每种可能的字段类型都有自己的类型并包含一个位置(可由用户自定义,因此它是 viewModel 的一部分,而不是视图。
Question part: What would be the best approach to implement the control?问题部分:实施控制的最佳方法是什么? I thought about
我想过
It depends on what the purpose of the control is, how it is supposed to work and how the UI is supposed to look like.这取决于控件的目的是什么,它应该如何工作以及 UI 应该是什么样子。
If you want to display several UserDefinedFieldBase
objects in the view you should use an ItemsControl that binds to the CustomFields property of the view model.如果您想在视图中显示多个
UserDefinedFieldBase
对象,您应该使用绑定到视图模型的 CustomFields 属性的 ItemsControl。 You could still add the ItemsControl to a UserControl with several other dependency properties though.不过,您仍然可以将 ItemsControl 添加到具有其他几个依赖项属性的 UserControl。
If you only want to display a list of UserDefinedFieldBase, you probably don't need a UserControl at all.如果您只想显示 UserDefinedFieldBase 的列表,您可能根本不需要 UserControl。 You could just use an ItemsControl and define the appearance of each item using the ItemTemplate.
您可以只使用 ItemsControl 并使用 ItemTemplate 定义每个项目的外观。
So you should use an ItemsControl but whether or not you need to create a UserControl is a bit unclear based on the information you have provided.因此,您应该使用 ItemsControl 但根据您提供的信息,您是否需要创建 UserControl 有点不清楚。 If you want to display something more than just an ItemsControl with a list of items, you could wrap the ItemsControl in a UserControl that adds several other elements as appropriate.
如果您想显示的不仅仅是带有项目列表的 ItemsControl,您可以将 ItemsControl 包装在 UserControl 中,该 UserControl 根据需要添加其他几个元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.