如何使用WPF工具包Datagrid DataGridTemplateColumn和组合框?
我有一个看起来像这样的数据网格
<tk:DataGrid ItemsSource="{Binding Parents}" AutoGenerateColumns="False">
<tk:DataGrid.Columns>
<tk:DataGridTextColumn Header="Description" Binding="{Binding ID}" />
<tk:DataGridTemplateColumn Header="Description" >
<tk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=Description, Mode=TwoWay}" />
</DataTemplate>
</tk:DataGridTemplateColumn.CellEditingTemplate>
<tk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Description}"/>
</DataTemplate>
</tk:DataGridTemplateColumn.CellTemplate>
</tk:DataGridTemplateColumn>
<tk:DataGridTemplateColumn Header="Child Description" >
<tk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox SelectedIndex="{Binding Path=ChildID}" ItemsSource="{Binding Path=Children}" />
</DataTemplate>
</tk:DataGridTemplateColumn.CellEditingTemplate>
<tk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Children.Description}"/>
</DataTemplate>
</tk:DataGridTemplateColumn.CellTemplate>
</tk:DataGridTemplateColumn>
</tk:DataGrid.Columns>
</tk:DataGrid>
视图绑定到一个 ViewModel,该 ViewModel 公开了一个父列表(应该是我的行)和一个子列表(应该是组合框下拉内容)。它的设置方式我得到了父母的行,但孩子描述列中没有数据。当我双击该行时,该行变得可编辑并且组合框出现。但没有数据。当我查看输出窗口时,我看到绑定错误“BindingExpression 路径错误:在‘对象’‘父级’上找不到‘子级’属性”。我知道...我如何告诉它向上一级查找?我尝试将数据网格绑定到视图模型,但没有显示任何行。我尝试过使用相对源标记,但仍然无法让它看到我想要它看到的内容。我确信我的语法不正确。我找不到任何例子。我做错了什么?
I have a data grid that looks like this
<tk:DataGrid ItemsSource="{Binding Parents}" AutoGenerateColumns="False">
<tk:DataGrid.Columns>
<tk:DataGridTextColumn Header="Description" Binding="{Binding ID}" />
<tk:DataGridTemplateColumn Header="Description" >
<tk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=Description, Mode=TwoWay}" />
</DataTemplate>
</tk:DataGridTemplateColumn.CellEditingTemplate>
<tk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Description}"/>
</DataTemplate>
</tk:DataGridTemplateColumn.CellTemplate>
</tk:DataGridTemplateColumn>
<tk:DataGridTemplateColumn Header="Child Description" >
<tk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox SelectedIndex="{Binding Path=ChildID}" ItemsSource="{Binding Path=Children}" />
</DataTemplate>
</tk:DataGridTemplateColumn.CellEditingTemplate>
<tk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Children.Description}"/>
</DataTemplate>
</tk:DataGridTemplateColumn.CellTemplate>
</tk:DataGridTemplateColumn>
</tk:DataGrid.Columns>
</tk:DataGrid>
The view is bound to a ViewModel that exposes a list of Parents which should be my rows and a Children list which is supposed to be the combobox dropdown contents. The way it is set up I get my rows of Parents, but no data in the Child Description column. When I double click the row becomes editable and the combobox shows up. But no data. When I look in the output window I see the binding error Saying "BindingExpression path error: 'Children' property not found on 'object' ''Parent' ". I know...How do I tell it to look up one level? I've tried binding the datagrid to just the viewmodel, but then no rows show up. I've tried using the relativesource markup and I still can't get it to see what I want it to see. I'm sure my syntax is incorrect. And I could not find any examples. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能有更聪明的方法来完成此任务,但我会使用的愚蠢而快速的方法是修改 Parent 对象以包含 Children 集合。这将使父子关系变得明确,并且您不必从上面更改您的 xaml 语法。
如果您不想将 Children 集合添加到 Parent 对象,您可以使用以下 xaml 绑定:
There may be smarter ways of accomplishing this task, but the stupid, quick method I would use is to modify the Parent object to contain a Children collection. This would make the relationship of Parent and Child explicit and you don't have to change your xaml syntax from above.
If you didn't want to add a Children collection to your Parent object, you may be able to use the following xaml binding::