创建 ObservableCollection班级
我有一个 person
类,想要创建一个名为 people
的 ObservableCollection
类。我看过一些类似实现的例子,但没有完全理解它们之间的差异。例如:
public class people : ObservableCollection<person>
{
public people()
{
}
}
?
public class people
{
ObservableCollection<person> public people()
{
}
}
这些实现之一从根本上来说是错误的吗 我没有任何工作代码,但希望朝着正确的方向推动开始。
编辑(根据@Groky的建议):
我希望创建一个ObservableCollection
。我想在带有绑定的 MVVM 设置中使用此集合。具体来说,我想将所有人员的名字和姓氏绑定到自定义控件。
首先:创建这样的类是不好的做法,还是应该按照 @Groky 的建议在视图模型中实现 ObservableCollection?
第二:使用 ObservableCollection 是否是解决此类问题的正确途径?
I have a person
class and would like to create and ObservableCollection<person>
class called people
. I have seen some examples of similar implementations, but don't fully understand the differences in them. For instance:
public class people : ObservableCollection<person>
{
public people()
{
}
}
and
public class people
{
ObservableCollection<person> public people()
{
}
}
Is one of these implementations fundamentally wrong? I do not have any working code, but would like a nudge in the right direction to start.
EDIT (per @Groky's suggestion):
I am looking to create a ObservableCollection<Person>
. I would like to use this collection in a MVVM setting with binding. Specifically, I would like to bind all Persons first and last name, for instance, to a custom control.
FIRST: Is it bad practice to create a class as such, or should I implement the ObservableCollection within the viewmodel as @Groky suggests?
SECOND: Is using an ObservableCollection even the correct route to solve this type of problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您正在将 MVVM 模式与 WPF 一起使用,并且您正在尝试创建一个 ViewModel 类。
正如前面的回答者提到的,您的两个选项描述了 1) 继承 2) 委托。选择哪一个取决于您想要表达的内容的含义。
如果您在逻辑上尝试向 People 集合中添加某些内容,则您会选择继承。但是,因为您暗示您想要绑定 WPF 中的其他属性,所以这表明您在逻辑上没有向集合添加属性;而是在 WPF 中添加属性。您还可以在集合旁边添加属性。因此,在本例中,您将使用 WPF 术语创建一个 ViewModel。
这是一个例子:
It sounds like you're using the MVVM pattern with WPF and that you're trying to create a ViewModel class.
As a previous answerer mentioned, your two options describe 1) Inheritence 2) Delegation. Which one to choose depends on the meaning of what you're trying to represent.
You would choose inheritence if you're logically trying to add something to the collection of People. However, because you hint you're wanting to bind the additional properties in WPF, that suggests to me that you're not logically adding a property to the collection; more you're adding a property alongside the collection. So in this case you're creating a ViewModel in WPF parlance.
Here's an example:
我认为你有多种选择,例如继承或委托模式。因此,在第一个变体中,您有继承模式,在第二个组合中,有混合和方面模式。
I think you have several choices, for example inheritance or delegation pattern. So in the 1st variant you have inheritance pattern, in the 2nd composition, mixin and aspect pattern.