WPF 数据网格组合框数据绑定

发布于 2024-10-01 05:32:36 字数 871 浏览 6 评论 0原文

谁能告诉我为什么这有效;

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小情绪 2024-10-08 05:32:37

这是因为 DataGridComboBoxColumn 不是用户界面元素,但 ComboBox 是。

在第一个示例中,由于您的 ComboBox 是可视化树的一部分,因此 RelativeSource 可以执行其应该执行的操作:沿着 UI 树向上查找您已找到的项目要求的。但在第二个示例中,DataGridComboBoxColumn 是一个DependencyObject,但它不是实际的 UI 元素 - 它是一个描述有关 UI 元素的内容的对象。

您可以尝试使用 ElementName 代替,并为根窗口指定一个名称。或者,您也许可以这样做:

<DataGridComboBoxColumn ...
   ItemsSource="{Binding Path=Suppliers}" />

DataContext 将从窗口向下流到网格,因此除非您此时在 UI 中用其他内容覆盖了它,否则它'仍然可用。

或者,如果这不起作用,您可能需要将相关集合添加到资源字典中,以便可以通过绑定中的 Source={StaticResource sellers} 获取它。

It's because a DataGridComboBoxColumn isn't a user interface element, but ComboBox 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, the DataGridComboBoxColumn is a DependencyObject 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:

<DataGridComboBoxColumn ...
   ItemsSource="{Binding Path=Suppliers}" />

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.

苦妄 2024-10-08 05:32:37

原因是找不到 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文