WPF 工具包 Accordion - 如何数据绑定
我一直在尝试(但失败了)使用数据绑定动态创建手风琴。
我有一个名为 MenuGroups 的集合,其中包含一个字符串“ModuleName”和一个名为 MenuItems 的 IList 集合。我希望将 MenuGroups 绑定到标题,将 MenuItems 绑定到内容。
到目前为止我管理的最接近的使用此 XAML:
<WPFToolkit:Accordion ItemsSource="{Binding MenuGroups}" HorizontalAlignment="Stretch" SelectionMode="OneOrMore">
<WPFToolkit:Accordion.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ModuleName}" />
</DataTemplate>
</WPFToolkit:Accordion.ItemTemplate>
<WPFToolkit:Accordion.ContentTemplate>
<DataTemplate>
<TextBox Text="{Binding MenuItems/MenuItemName}"/>
</DataTemplate>
</WPFToolkit:Accordion.ContentTemplate>
</WPFToolkit:Accordion>
这会正确生成标题,但内容中仅显示每个组中的第一个菜单项。我已经尝试了上述各种不同的含义,但到目前为止我还没有达到预期的结果。我在内容模板中尝试使用 ListView 而不是 TextBlock,认为我需要它来显示多个项目,但这会产生一个空白内容区域。
有人可以帮忙吗?
I have been trying (and failing) to dynamically create an accordion using databinding.
I have a collection called MenuGroups, which contains a string 'ModuleName' and an IList collection called MenuItems. I wish to bind the MenuGroups to the headers and the MenuItems to the content.
The closest I have managed so far uses this XAML:
<WPFToolkit:Accordion ItemsSource="{Binding MenuGroups}" HorizontalAlignment="Stretch" SelectionMode="OneOrMore">
<WPFToolkit:Accordion.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ModuleName}" />
</DataTemplate>
</WPFToolkit:Accordion.ItemTemplate>
<WPFToolkit:Accordion.ContentTemplate>
<DataTemplate>
<TextBox Text="{Binding MenuItems/MenuItemName}"/>
</DataTemplate>
</WPFToolkit:Accordion.ContentTemplate>
</WPFToolkit:Accordion>
This produces the headers correctly, but only the first menu item in each group is displayed in the content. I have tried various different connotations of the above, but as yet I have not achieved the desired result. I tried a ListView instead of a TextBlock in the content template thinking I would need that for multiple items,but that produced a blank content area.
Can anybody help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
属性路径中的
/
字符表示绑定到集合中的当前项。由于您没有以其他方式设置当前项目,因此它始终只是第一个项目。请参阅 PropertyPath XAML 语法。如果您希望内容是菜单项的整个列表,则应使用
ItemsControl
或其子类之一,例如列表框
。像这样的东西将为集合中的每个菜单项提供一个文本框:
The
/
character in a property path means bind to the current item in a collection. Since you aren't setting the current item in some other way, it will always just be the first item. See PropertyPath XAML Syntax.If you want the content to be the entire list of MenuItems, you should use an
ItemsControl
, or one of its subclasses such asListBox
.Something like this will give you a text box for every MenuItem in the collection: