将数据网格的选定项绑定到树视图的选定值时出现问题
我在将树视图绑定到数据网格的选定项目时遇到问题。
它们位于不同的视图中,但数据网格的所选项目已传递到树视图的相关视图模型。 TreeView相关的ViewModel中有一个SelectedGroup属性,它是DataGrid的选中项,其类型是Group。我想将组的 ID 字段绑定到树视图,即我希望在树视图中选择所选项目的 ID,并通过树视图的所选值进行更新。 我找不到如何绑定。 这是我的树视图的骨架,它可以按层次结构列出所有组。 有人可以帮我填写必填字段吗? 提前致谢。
<TreeView Grid.Column="1" Grid.Row="4" Height="251" HorizontalAlignment="Left"
Margin="4,3,0,0" Name="parentGroupTreeView" VerticalAlignment="Top"
Width="246" ItemsSource="{Binding Groups}" ItemContainerStyle="{x:Null}"
SelectedValuePath="ID">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding
Converter={x:Static Member=conv:GroupSubGroupsConv.Default}}">
<Label Name="groupLabel" Content="{Binding GroupName}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
I have problems with binding a treeview to a datagrid's selected item.
they are in different views, but datagrid's selected item is already passed to treeview's related viewmodel.
There is a SelectedGroup property in treeview's related viewmodel which is datagrid's selected item and its type is Group. I want to bind the ID field of Group to treeview, i.e. I want the ID of selected item to be selected in treeview and also be updated by selected value of treeview.
I couldn't find out how to bind.
Here's my treeview's skeleton, which can just lists all of the groups hierarchically.
Can anyone help me on filling the required fields please?
Thanks in advance.
<TreeView Grid.Column="1" Grid.Row="4" Height="251" HorizontalAlignment="Left"
Margin="4,3,0,0" Name="parentGroupTreeView" VerticalAlignment="Top"
Width="246" ItemsSource="{Binding Groups}" ItemContainerStyle="{x:Null}"
SelectedValuePath="ID">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding
Converter={x:Static Member=conv:GroupSubGroupsConv.Default}}">
<Label Name="groupLabel" Content="{Binding GroupName}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先查看 Josh Smith 撰写的以下文章:通过使用视图模型模式。
我还使用 WPF 工具包中的 DataGrid。
要了解此代码的工作原理,请查看下面的 IsSelected 属性。
这是包含树和数据网格的 XAML:
这是 TreeView 和 DataGrid 绑定到的主视图模型:
这是我用来绑定到网格和树的视图模型:
为了完整起见,这里是 Group 对象:
还有 TreeView 的基类:
Start by taking a look at the following article by Josh Smith on Simplifying the WPF TreeView by Using the ViewModel Pattern.
I am also using the DataGrid from the WPF toolkit.
To get a sense as to how this code works look at the IsSelected property below.
Here is the XAML that contains a tree and a datagrid:
Here is the main view model that the TreeView and DataGrid bind to:
Here is the viewmodel that I use to bind to the grid and the tree:
For completeness here is the Group object:
And also the base class for the TreeView: