WPF Datagrid ComboBox DataBinding

Can someone 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 is not so:

<DataGridComboBoxColumn Header="Combo" DisplayMemberPath="SupplierName" SelectedValuePath="SupplierID" 
  SelectedValueBinding="{Binding SupplierID}"
  ItemsSource="{Binding Path=DataContext.Suppliers, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />

The second fragment does not display the source_name list when editing ...

+3
source share
2 answers

This is because it is DataGridComboBoxColumnnot a user interface element, but it ComboBoxdoes.

, ComboBox , RelativeSource , : , . DataGridComboBoxColumn DependencyObject, - , - .

ElementName . , , :

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

DataContext , , - , .

, , , Source={StaticResource suppliers} .

+5

, ItemsSource DataGridComboBoxColumn .

RelativeSource DataContext AncestorType. , DataContext, , ItemsSource.

0

Source: https://habr.com/ru/post/1773545/


All Articles