3

我在从 DataGrid 中访问 Window 的 DataContext 时遇到了一些问题。

DataGrid 绑定到 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();
    }

从我的 xaml 中,我尝试获取这些数据来填充 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}" />

但它不起作用。我已经尝试了很多变种。ComboBox 未被填充。
非常感谢任何帮助!

笔记:

同一窗口中的以下 ComboBox 确实有效:

<ComboBox x:Name="workingCombo" ItemsSource="{Binding Path=Items}" DisplayMemberPath="Description" SelectedValuePath="ItemID" />
4

1 回答 1

1

DataGridComboBoxColumn没有直接连接到可视树,因此 -操作FindAncestor将失败(并且 DataContext 也不会被继承)。

  • 最简单的解决方案是 为每一行创建一个 ViewModel并在 ItemsSource 中为 ComboBox 提供。
  • 使用 aDataGridTemplateColumn并将其放入ComboBox帮助中 DataTemplate
  • 这是关于此问题的另一篇文章。并且看看这个帖子。
于 2010-08-03T18:54:05.677 回答