绑定到类项目

发布于 2024-12-07 11:02:32 字数 768 浏览 0 评论 0原文

这看起来应该是非常基本的,但我似乎无法让它工作。

我有一个名为 XMLSource 的类文件,如下所示:

Public Class XMLSource

    Public Shared BrandItems As New MediaItems

    Public Class MediaItems
        Inherits ObservableCollection(Of MediaObject)
        Implements INotifyPropertyChanged   
    End Class
End Class

Public Class MediaObject
    Public Property Name As String
    Public Property Title As String
End Class

应用程序读取 XML 文件并将一些项目存储到 XMLSource.BrandItems 中(在启动时发生)。

我想将 Label 控件的 Content 属性绑定到 XMLSource.BrandItems(0).Name

我尝试过:

<Label Content="{Binding Source={XMLSource},Path=.BrandItems[0].Src}" FontSize="20"></Label>

但它不起作用。

这样直接绑定可以吗?

This seems like it should be really basic but I can't seemto get it working.

I have a class file called XMLSource as follows:

Public Class XMLSource

    Public Shared BrandItems As New MediaItems

    Public Class MediaItems
        Inherits ObservableCollection(Of MediaObject)
        Implements INotifyPropertyChanged   
    End Class
End Class

Public Class MediaObject
    Public Property Name As String
    Public Property Title As String
End Class

The application reads an XML file and stores some items into XMLSource.BrandItems (happens on start-up).

I want to bind a Label control's Content property to XMLSource.BrandItems(0).Name

I tried:

<Label Content="{Binding Source={XMLSource},Path=.BrandItems[0].Src}" FontSize="20"></Label>

But it's not working.

Is it possible to bind directly like this?

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

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

发布评论

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

评论(1

愛上了 2024-12-14 11:02:32

如果您编写的 {} 指示 标记扩展,此外,绑定路径中不能有静态/共享成员。我认为正确的绑定是:

{Binding [0].Src, Source={x:Static ns:XMLSource.BrandItems}}

x:Static< /a> 是一个允许访问静态成员的标记扩展。 (请注意,这还允许访问与仅允许公共属性的 Path 不同的字段)

其中 nsxmlns 属性 并指向 XMLSource 类的命名空间。

You cannot contruct bindings like this, if you write {} that indicates a markup extension, further you cannot have static/shared members in a binding path. I think the correct binding would be:

{Binding [0].Src, Source={x:Static ns:XMLSource.BrandItems}}

x:Static is a markup extension which allows access of static members. (Note that this also allows access of fields unlike the Path which only allows public properties)

Where ns is declared in an xmlns attribute and points to the namespace of your XMLSource class.

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