WP7 - 列表框绑定
我有一个 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}"/>
我做错了什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将您的绑定路径更改为:
然后在您的代码中执行此操作(在页面构造函数中的InitializeComponents之后):
免费书籍,作者:Charles Petzold:第 12 章,第 363 页。
您还可以从列表框中删除
DataContext
属性并设置DataContext。如果您创建一个新的数据绑定应用程序,您将通过其默认示例明白我的意思。
Change your Binding Path to this:
Then in your code do this (after InitializeComponents in the page constructor):
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 theDataContext
of the page itself. If you create a new databound application, you will see what I mean with their default example.