質問

I have some problem to access the Window's DataContext from within a DataGrid.

The DataGrid is bound to a 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 within my xaml I try to get those data to fill 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 it doesn't work. I tried out many variants already. The ComboBox gets not populated.
Any help greatly appreciated!

Note:

The following ComboBox in the same Window does work:

<ComboBox x:Name="workingCombo" ItemsSource="{Binding Path=Items}" DisplayMemberPath="Description" SelectedValuePath="ItemID" />
役に立ちましたか?

解決

The DataGridComboBoxColumn is not directly connected to the visual tree and therefore the FindAncestor-operation will fail (and also the DataContext will not be inherited).

  • The most simple solution is to create a ViewModel for each line and provide there in the ItemsSource for the ComboBox.
  • Using a DataGridTemplateColumn and placing the ComboBox in the DataTemplate helps.
  • Here is a another post concerning this problem. And look also at this post.
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top