3

DataGrid内からウィンドウの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を作成し、ComboBoxのItemsSourceで提供することです。
  • を使用しDataGridTemplateColumnてヘルプに配置しComboBoxます DataTemplate
  • この問題に関する別の投稿があります。そしてまた見てくださいこの郵便受け。
于 2010-08-03T18:54:05.677 に答える