WPF/mvvm 中的动态选项卡控件
我正在遵循 wpf/mvvm 方法。我需要一个选项卡控件...具有多行...我的意思是父/子行。当用户单击某个选项卡时,应显示该选项卡下的所有子选项卡...但应仅加载一个子选项卡下的页面。
有人可以帮我解决这个问题吗?
I am following wpf/mvvm method. I need to have a tab control...with multiple rows..i mean parent/child rows. When user click on a tab, all child tabs under that should be displayed...but only the page under one child tab should be loaded.
Can anyone help me regarding this please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您可以通过将选项卡控件中的 SelectedTabIndex 属性绑定到 ViewModel 中的字符串来做到这一点。 (因此,整个问题在于如何设计 ViewModel 中的数据结构。)
关于在选择 Tab 时加载 Tab 内容:
最初,您可以从 ViewModel 调用服务并获取选项卡的数量,将其填充到 Observable 集合中并将您的选项卡绑定到集合。您可以在 ViewModel 中拥有一个名为 SelectedTab 的属性,并将其值为 -1。现在,在获得选项卡数量并将这些选项卡绑定到选项卡控件后(我没有加载选项卡的内容;仅加载用于显示的选项卡),您可以设置 SelectedTab=0 并在设置器中传递 TabId 和加载选项卡的数据。
但是请确保您没有太多选项卡,因为选项卡太多且每个选项卡内的数据太多,应用程序的性能将非常糟糕,因为每次选择不同的选项卡时,WPF 都会清除选项卡视觉树中旧选项卡的内容。
Yes you can do that by binding SelectedTabIndex property in tab Control to a string in ViewModel. ( So the whole thing lies in how you design the data structures in the ViewModel.)
Regarding loading of Tab content on selection of Tab :
Initially you can call a service from your ViewModel and get the number of tabs,populate it to a Observable collection and bind your Tabs to the collection. You can have a property called SelectedTab in your ViewModel and have its value as -1. Now after you have got the number of tabs and bound those tabs to the tab control (I am not loading the contents of tab; only the tabs for display) , you can set SelectedTab=0 and in the setter you can pass the TabId and load the data for the Tab.
But make sure you dont have too many Tabs cos the application will perform very badly with too many tabs and too much data inside each tab because everytime when you select a different Tab, WPF will clear the contents of older Tab from visual tree.
编辑
您应该持有一个父选项卡,该选项卡将持有选项卡并将绑定到该集合,
如果您持有,问题就出在该集合上
公共 ObservableCollection>我的集合 { 获取;放; } 你必须同时持有它们。
因此,我建议您保留
IEnumerable _modules
,每次加载另一个选项卡时,您都会动态执行 IEnumerable _modules
清除(您可以通过使用触发器和命令来做到这一点),您可以为每个项目调用 dispoe
希望这次我理解您
EDIT
you should hold a father tab which will hold tabs and would be binding to this collection
where the problem is if you hold
public ObservableCollection> MyCollection { get; set; } you have to hold them all in the same time .
therefore I would recommend you to hold
IEnumerable _modules
where every time you load antoher tab you would dynamicaly do IEnumerable _modules
clear(you can do it when by using trigger and comand ) where you would call dispoe for each item
hope this time i understood you