WPF 多个主/详细信息,同一网格
我有一个具有三个级别的 TreeView。
假设它是一个联赛、分区和球队 TreeView
。
现在,当我选择树中的每个项目时,我想查看有关它的详细信息。
实现这一目标的最佳方法是什么?
由于 Grid
没有项目(如 ListBox
),我不能只设置其 ItemsSource
并创建 DataTemplate...
我考虑过使用 ListBox
,它仅包含所选项目,但这似乎非常糟糕...
谢谢。
I have a TreeView
which has three levels.
Lets say its a league, division and team TreeView
.
Now, when I select each of the items in the tree, I would like to see detailed information about it.
Whats the best way to achieve this?
Since Grid
doesn't items (like a ListBox
), I can't just set its ItemsSource
and make a DataTemplate
...
I thought about using a ListBox
which will contain only the selected item but this seems to be very bad...
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您首先为您的联赛、分区和球队类别定义 3 个
DataTemplate
。之后,将TreeView
绑定到对象的根。您的 League 和 Division 类应该具有返回子级的 Children 属性。您的所有类都应该有一个 Name 属性。然后,当您想要显示单个对象时,请使用
ContentPresenter
,并将其内容绑定到SelectedItem
(如果是TreeView
)。例如:
此代码未经测试或编译,仅作为示例提供。
You first define the 3
DataTemplate
s for your league, division and team classes. After, you bind theTreeView
to the root of your objects. Your League and Division classes should have a Children property that returns the children. All your classes should have a Name property.Then when you want to show a single object, use the
ContentPresenter
, and the bind its content to theSelectedItem
if theTreeView
.For example:
This code was not tested or compiled, it just provided as an example.
我将创建一个视图模型,其中包含树结构、当前选择和当前选择的详细信息的属性。
树结构是单向绑定到树视图的,树视图中的 SelectedItem 是 OneWayToSource 绑定到当前选择属性的(由于该属性的限制)。一旦当前选择更改,此属性就会更改子项列表,并且子项将绑定到显示它们的列表框。
I would create a viewmodel with properties for the tree structur, the current selection and the details to the current selection.
The tree struct is one-way bound to the treeview, the SelectedItem from the treeview is OneWayToSource bound to the current selection property (due to the limitations of the property). This property changes the list of child items once the current selection changes, and the child items are bound to the listbox displaying them.