WPF 数据网格组合框数据绑定
谁能告诉我为什么这有效;
<DataGridTemplateColumn Header="Supplier">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox DisplayMemberPath="SupplierName" SelectedValuePath="SupplierID"
SelectedValue="{Binding SupplierID}"
ItemsSource="{Binding Path=DataContext.Suppliers, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
但这不是;
<DataGridComboBoxColumn Header="Combo" DisplayMemberPath="SupplierName" SelectedValuePath="SupplierID"
SelectedValueBinding="{Binding SupplierID}"
ItemsSource="{Binding Path=DataContext.Suppliers, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
第二个片段不显示编辑时的供应商名称列表...
Can anyone tell me why this works;
<DataGridTemplateColumn Header="Supplier">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox DisplayMemberPath="SupplierName" SelectedValuePath="SupplierID"
SelectedValue="{Binding SupplierID}"
ItemsSource="{Binding Path=DataContext.Suppliers, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
but this doesn't;
<DataGridComboBoxColumn Header="Combo" DisplayMemberPath="SupplierName" SelectedValuePath="SupplierID"
SelectedValueBinding="{Binding SupplierID}"
ItemsSource="{Binding Path=DataContext.Suppliers, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
Second snippet does not show the list of SupplierName on edit...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为
DataGridComboBoxColumn
不是用户界面元素,但ComboBox
是。在第一个示例中,由于您的
ComboBox
是可视化树的一部分,因此RelativeSource
可以执行其应该执行的操作:沿着 UI 树向上查找您已找到的项目要求的。但在第二个示例中,DataGridComboBoxColumn
是一个DependencyObject
,但它不是实际的 UI 元素 - 它是一个描述有关 UI 元素的内容的对象。您可以尝试使用
ElementName
代替,并为根窗口指定一个名称。或者,您也许可以这样做:DataContext
将从窗口向下流到网格,因此除非您此时在 UI 中用其他内容覆盖了它,否则它'仍然可用。或者,如果这不起作用,您可能需要将相关集合添加到资源字典中,以便可以通过绑定中的
Source={StaticResource sellers}
获取它。It's because a
DataGridComboBoxColumn
isn't a user interface element, butComboBox
is.In the first example, because your
ComboBox
is part of the visual tree,RelativeSource
can do what it's supposed to do: walk up the UI tree looking for the item you've asked for. But in the second example, theDataGridComboBoxColumn
is aDependencyObject
but it's not an actual UI element - it's an object that describes something about the UI element.You could try using
ElementName
instead, and give a name to your root window. Or, you might be able to get away with just:The
DataContext
will flow down from the window to the grid, so unless you've overidden it with something else at this point in the UI, it'll still be available.Or if that doesn't work, you might want to add the relevant collection to a resource dictionary so you can get it with a
Source={StaticResource suppliers}
in the binding.原因是找不到 DataGridComboBoxColumn 的 ItemsSource。
您将需要使用RelativeSource Binding 并将其指向正确的DataContext AncestorType。这将需要一些尝试和错误才能找到包含您的列表的 DataContext 以满足您的 ItemsSource。
The reason is that the ItemsSource for the DataGridComboBoxColumn can not be found.
You will need to use the RelativeSource Binding and point it to the correct DataContext AncestorType. This will take some trial and error to find the DataContext that contains your list to satisfy your ItemsSource.