如何将多个类添加到可观察集合中

发布于 2024-12-29 13:44:03 字数 1239 浏览 0 评论 0原文

我仍在自学如何绑定和使用可观察集合。我有点困惑的一个问题是将多个类/可观察集合绑定到一页。换句话说,如果我有一个 PersonName 类和一个 AnimalName 类,我必须为每个类创建两个单独的 observalbe 集合?当页面只允许一个时,我该如何设置数据上下文?

例如:

       Public Class NameList
Inherits ObservableCollection(Of PersonName)

' Methods
Public Sub New()
    MyBase.Add(New PersonName("Willa", "Cather"))
    MyBase.Add(New PersonName("Isak", "Dinesen"))
    MyBase.Add(New PersonName("Victor", "Hugo"))
    MyBase.Add(New PersonName("Jules", "Verne"))
End Sub

End Class

Public Class PersonName
' Methods
Public Sub New(ByVal first As String, ByVal last As String)
    Me._firstName = first
    Me._lastName = last
End Sub


' Properties
Public Property FirstName() As String
    Get
        Return Me._firstName
    End Get
    Set(ByVal value As String)
        Me._firstName = value
    End Set
End Property

Public Property LastName() As String
    Get
        Return Me._lastName
    End Get
    Set(ByVal value As String)
        Me._lastName = value
    End Set
End Property


' Fields
Private _firstName As String
Private _lastName As String
 End Class

现在如果我添加另一个类,我将如何在绑定部分和集合部分上将两者结合起来。感谢您的任何建议:)

public class AnimalName 
  'properties, ect...ect..ect..
End class

I'm still teaching myself how to bind and use observable collection. One problem that I'm a little confused on is binding multiple classes/observable collection to one page. In other words, if I have a PersonName class and a AnimalName class, I have to create two separate observalbe collections for each? How would I set the datacontext when a page only allows one?

For example:

       Public Class NameList
Inherits ObservableCollection(Of PersonName)

' Methods
Public Sub New()
    MyBase.Add(New PersonName("Willa", "Cather"))
    MyBase.Add(New PersonName("Isak", "Dinesen"))
    MyBase.Add(New PersonName("Victor", "Hugo"))
    MyBase.Add(New PersonName("Jules", "Verne"))
End Sub

End Class

Public Class PersonName
' Methods
Public Sub New(ByVal first As String, ByVal last As String)
    Me._firstName = first
    Me._lastName = last
End Sub


' Properties
Public Property FirstName() As String
    Get
        Return Me._firstName
    End Get
    Set(ByVal value As String)
        Me._firstName = value
    End Set
End Property

Public Property LastName() As String
    Get
        Return Me._lastName
    End Get
    Set(ByVal value As String)
        Me._lastName = value
    End Set
End Property


' Fields
Private _firstName As String
Private _lastName As String
 End Class

Now If I add another class, how would I combine the two on the binding part and collection part. Thanks for any advice :)

public class AnimalName 
  'properties, ect...ect..ect..
End class

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一梦等七年七年为一梦 2025-01-05 13:44:03

您需要将设置为 DataContext 的对象设为另一个类,其中包含人和动物的集合。然后,显示列表的控件可以将其 ItemsSource 属性绑定到 DataContext 中的相关属性,例如

ItemsSource="{Binding Path=Animals}"

您不能将它们添加到同一个集合中,除非它是它们都派生自的某种类型的集合(最终所有内容都派生自Object),但随后您必须处理如何在 UI 中正确呈现它们。

此外,虽然页面有一个 DataContext,但其中的所有内容也都有一个 DataContext,因此您可以单独绑定它们 - 否则它们只是从其父级继承 DataContext。

You need to make the object which you set as the DataContext to be another class which contains the collection of people and the collection of animals. Then your controls which display the lists can bind their ItemsSource properties to the relevant properties in the DataContext, such as

ItemsSource="{Binding Path=Animals}"

You can't add them both to the same collection unless it's a collection of some type that they both derive from (ultimately everything derives from Object), but then you have to deal with how to render them appropriately in the UI.

Also although the page has a DataContext, everything inside it has a DataContext too, so you can bind those individually - otherwise they just inherit the DataContext from their parent.

好听的两个字的网名 2025-01-05 13:44:03

您应该将各个列表控件的 ItemSource 设置为单独的 ObservableCollections。

You should set the ItemSources of the individual list controls to separate ObservableCollections.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文