在 XAML 中间接绑定到子集合
我目前正在使用带有 SubSonic3 的 Linq 模板来创建我的模型。 我有一个简单的会员类,其中有一个注释集合。 Note 类具有 PublishedDate 和 Title 属性。
我有一个 UserControl,它有一个 DataContext 成员集合。 上面有两个 ListBoxes
和一堆 TextBoxes
以及其他控件。 第一个 ListBox
(membersList) 从 UserControl
DataContext 获取其 ItemsSource
,并在每个项目中显示一些成员属性。 我希望第二个 ListBox
(notesList) 显示第一个 ListBox
中所选成员的 Notes 集合。
到目前为止,我在notesList上有:
<ListBox ... ItemsSource="{Binding Notes}"...>
<DataTemplate>
<TextBox Text="{Binding Title}"/>
</DataTemplate>
它部分有效。 当我选择具有注释项目的成员时,notesListBox 中的项目数量正确,但这仅在交替行颜色中可见 - 没有其他内容! DataTemplate
无法呈现注释的标题。 基本上,ListBox
和 ListBoxItem
中的所有视觉样式都有效 - 交替颜色、选择边框、鼠标悬停等,但不显示内容。 我感觉问题出在我的绑定语法上,但我不知道出了什么问题。
任何建议将不胜感激。
干杯, 丹妮.
I am currently using a Linq template with SubSonic3 to create my models. I have a simple Member class, which has a collection of Notes. A Note class has a PublishedDate and Title properties.
I have a UserControl which has a collection of Members for its DataContext
. There are two ListBoxes
and a bunch of TextBoxes
and other controls on it. The first ListBox
(membersList) gets its ItemsSource
from the UserControl
DataContext and it displays a few Member properties in each item. I would like the second ListBox
(notesList) to display the Notes collection of the Member selected in the first ListBox
.
So far on notesList I have:
<ListBox ... ItemsSource="{Binding Notes}"...>
<DataTemplate>
<TextBox Text="{Binding Title}"/>
</DataTemplate>
It partially works. When I selected a Member which has Note items there is the correct number of items in notesListBox
but this is only visible in the alternating row colours - nothing else! The DataTemplate
fails to render the Title of the Note. Basically all the visual styles in the ListBox
and ListBoxItem
works - alternating colours, selection border, mouseover, etc. but the content is not shown. I have a feeling the problem is my Binding syntax but I can't figure out what's wrong.
Any suggestions would be greatly appreciated.
Cheers,
Dany.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保您的模板包含 ContentPresenter。 听起来您在模板化列表框时可能忽略了这一点。
忽略这一点可能会导致您遇到的行为 - 完整的模板,但没有“内容”,因为它没有地方放置应放入每个列表框项目中的实际项目。
Make sure your template includes a ContentPresenter. It sounds like you may have left this out when you templated your list box.
Leaving this out could lead to the behavior you're experiencing - full template, but no "content", since there's no place for it to place the actual items that should go in each list box item.