Access DataContext from DataGrid

I have some problem accessing a Window DataContext from a DataGrid.

DataGrid bound to IBindingList:

public IBindingList Items{ get; set; }
    private void initItems()
    {
        //ItemFactory is a Linq2SQL Context, Items is the view of availabe Items
        this.Items = this.ItemFactory.Items.GetNewBindingList();
    }

From my xaml, I'm trying to get this data to populate a ComboBox:

 <DataGridComboBoxColumn Header="Typ" 
                             DisplayMemberPath="Description"
                             SelectedValuePath="ItemID"      
                             ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Mode=OneWay, Path=DataContext.Items, UpdateSourceTrigger=PropertyChanged}" />

But that will not work. I have already tried many options. ComboBox is not filling.
Any help is much appreciated!

Note:

The following ComboBox in the same window works:

<ComboBox x:Name="workingCombo" ItemsSource="{Binding Path=Items}" DisplayMemberPath="Description" SelectedValuePath="ItemID" />
+3
source share
1 answer

DataGridComboBoxColumnis not directly connected to the visual tree, and therefore, the FindAncestoroperation will fail (as well as the DataContext will not be inherited).

  • ViewModel ItemsSource ComboBox.
  • DataGridTemplateColumn ComboBox DataTemplate .
  • < href= "http://joemorrison.org/blog/2009/02/17/excedrin-headache-35401281-using-combo-boxes-with-the-wpf-datagrid" rel= "nofollow noreferrer" > , . this .
+1

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


All Articles