谁能告诉我为什么这有效;
<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}}}" />
第二个片段不显示编辑时的供应商名称列表...
这是因为一个DataGridComboBoxColumn
不是用户界面元素,但是ComboBox
is.
在第一个例子中,因为你的ComboBox
是视觉树的一部分,RelativeSource
可以做它应该做的事情:沿着 UI 树查找您要求的项目。但在第二个例子中,DataGridComboBoxColumn
is a DependencyObject
但它不是一个实际的 UI 元素 - 它是一个描述 UI 元素相关内容的对象。
你可以尝试使用ElementName
相反,并为您的根窗口命名。或者,您也许可以通过以下方式逃脱:
<DataGridComboBoxColumn ...
ItemsSource="{Binding Path=Suppliers}" />
The DataContext
将从窗口向下流到网格,因此除非您此时在 UI 中用其他内容覆盖了它,否则它仍然可用。
或者,如果这不起作用,您可能需要将相关集合添加到资源字典中,以便可以使用Source={StaticResource suppliers}
在绑定中。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)