简体   繁体   English

如何基于绑定列表构建包含其他控件的 WPF 控件

[英]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我想过

  • Either creating a UserControl with a DependencyProperty of type ObservableCollection<UserDefinedFieldBase> and use some appropriate container with a TemplateSelector for the derived classes创建一个具有 ObservableCollection<UserDefinedFieldBase> 类型的 DependencyProperty 的 UserControl 并使用一些适当的容器与派生类的 TemplateSelector
  • Work/implement a ItemsControl and ItemsSource without going through a custom DependencyProperty工作/实现 ItemsControl 和 ItemsSource 而不通过自定义 DependencyProperty

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.

相关问题 WPF-根据其他控件更改绑定数据 - WPF - changing bound data according to other controls 如何将ListView控件置于WPF中的其他控件之上 - How I can bring ListView control in top of other controls in WPF WPF-绑定到同一控件实例的两个用户控件的内容 - WPF - Content of two user controls bound to same instance of control 在WinForms中集成WPF控件而不允许访问其他WPF控件? - Integrating a WPF control in WinForms without allowing access to other WPF controls? WPF移动控件基于其他控件 - WPF Move control based on other control 如何在WPF控件/控件集中获得与具有链接到其他页面内容的HTML相同的行为? - How can I get the same behavior in a WPF control/set of controls as that of HTML with links to other page content? 如何检测WPF控件的可见区域/区域与其他控件重叠考虑透明度? - How to retrieve the visible area/areas of a WPF control overlapped by other controls considering transparency? c# WPF - 如何在不阻止其他控件可点击的情况下识别控件外的鼠标单击 - c# WPF - How to recognize mouse click outside control without blocking other controls from being clickable WPF绑定数据未加载到绑定控件 - WPF bound data not loading to bound controls WPF窗口-添加超链接控件后,其他控件对点击没有响应 - WPF window - other controls not responding to clicks after adding Hyperlink control
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM