WP7 - 列表框绑定

发布于 2024-10-10 03:46:46 字数 1829 浏览 0 评论 0 原文

我有一个 ObservableCollection,我想绑定到我的列表框...

lbRosterList.ItemsSource = App.ViewModel.rosterItemsCollection;

但是,在该集合中我还有另一个集合:

 [DataMember]
    public ObservableCollection<PersonDetail> ContactInfo
    {
        get
        {
            return _ContactInfo;
        }
        set
        {
            if (value != _ContactInfo)
            {
                _ContactInfo = value;
                NotifyPropertyChanged("ContactInfo");
            }
        }
    }

PersonDetail 包含 2 个属性:姓名和电子邮件

我希望列表框具有 rosterItemsCollection 中每个项目的这些值

RosterId = 0;
RosterName = "test";
ContactInfo.name = "Art";
ContactInfo.email = "[email protected]";

RosterId = 0;
RosterName = "test"
ContactInfo.name = "bob";
ContactInfo.email = "[email protected]";

RosterId = 1;
RosterName = "test1"
ContactInfo.name = "chris";
ContactInfo.email = "[email protected]";

RosterId = 1;
RosterName = "test1"
ContactInfo.name = "Sam";
ContactInfo.email = "[email protected]";

我希望该列表框显示 ContactInfo 信息。

我希望这是有道理的...

我尝试过的 XAML 收效甚微:

<listbox x:Name="lbRosterList" ItemsSource="rosterItemCollection">
<textblock x:name="itemText" text="{Binding Path=name}"/>

我做错了什么?

I have an ObservableCollection that I want to bind to my listbox...

lbRosterList.ItemsSource = App.ViewModel.rosterItemsCollection;

However, in that collection I have another collection within it:

 [DataMember]
    public ObservableCollection<PersonDetail> ContactInfo
    {
        get
        {
            return _ContactInfo;
        }
        set
        {
            if (value != _ContactInfo)
            {
                _ContactInfo = value;
                NotifyPropertyChanged("ContactInfo");
            }
        }
    }

PersonDetail contains 2 properties: name and e-mail

I would like the listbox to have those values for each item in rosterItemsCollection

RosterId = 0;
RosterName = "test";
ContactInfo.name = "Art";
ContactInfo.email = "[email protected]";

RosterId = 0;
RosterName = "test"
ContactInfo.name = "bob";
ContactInfo.email = "[email protected]";

RosterId = 1;
RosterName = "test1"
ContactInfo.name = "chris";
ContactInfo.email = "[email protected]";

RosterId = 1;
RosterName = "test1"
ContactInfo.name = "Sam";
ContactInfo.email = "[email protected]";

I would like that listboxes to display the ContactInfo information.

I hope this makes sense...

My XAML that I've tried with little success:

<listbox x:Name="lbRosterList" ItemsSource="rosterItemCollection">
<textblock x:name="itemText" text="{Binding Path=name}"/>

What am I doing incorrectly?

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

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

发布评论

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

评论(1

不必了 2024-10-17 03:46:46

将您的绑定路径更改为:

<ListBox x:Name="lbRosterList" DataContext="{Binding}" ItemsSource="{Binding rosterItemCollection}">
<TextBlock x:Name="itemText" Text="{Binding Path=ContactInfo.name}"/>

然后在您的代码中执行此操作(在页面构造函数中的InitializeComponents之后):

lbRosterList.DataContext = App.ViewModel;

免费书籍,作者:Charles Petzold:第 12 章,第 363 页。

您还可以从列表框中删除 DataContext 属性并设置 DataContext。如果您创建一个新的数据绑定应用程序,您将通过其默认示例明白我的意思。

Change your Binding Path to this:

<ListBox x:Name="lbRosterList" DataContext="{Binding}" ItemsSource="{Binding rosterItemCollection}">
<TextBlock x:Name="itemText" Text="{Binding Path=ContactInfo.name}"/>

Then in your code do this (after InitializeComponents in the page constructor):

lbRosterList.DataContext = App.ViewModel;

Excellent reference for this kind of thing in this free book by Charles Petzold: Chapter 12, pg 363.

You can also remove the DataContext attribute from the listbox and set the DataContext of the page itself. If you create a new databound application, you will see what I mean with their default example.

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