2016-06-01 3 views
0

List<Coefficients>にはDataGridが割り当てられています。 CoefficientsクラスにはIDとしてstring Nameがあり、他の多くのフィールド(コードのプライバシーを保護するためにいくつかの名前と識別情報が変更されています)があります。 DataGridコードは次のようである:同じデータグリッド内の別の列の値がデータグリッドのコンボボックスに設定されています

<DataGrid 
    Name="coefficientList" 
    ItemsSource="{Binding Data.CoefficientList, Mode=TwoWay}"> 
    <DataGrid.Columns> 
     <DataGridTextColumn Binding="{Binding Path=Name, Mode=TwoWay}" /> 
     <DataGridTextColumn Binding="{Binding Path=Priority, Mode=TwoWay}" /> 
     ... 
    </DataGrid.Columns> 
</DataGrid> 

今DataGrid内の要素は、同じコレクション内の別の要素を参照する必要がありますので、私はCoefficientsクラスに新しいフィールドを持つ:string ReferencedName。これは、コレクション内の参照される要素の文字列IDです。だから私は、コンボボックスを介して、参照された要素の値を選択するデータグリッドの新しい列を持っていると同じデータグリッドのNameの列でコンボボックスを設定する必要があります。

どうすれば実現できますか?

NameList

public IEnumerable<string> NameList 
{ 
    get 
    { 
     return CoefficientList.Select(c => c.Name); 
    } 
} 

ですが、私がこれまで試してみました何かが常にオプションなしでコンボボックスを与え、

のような出力ウィンドウにエラー
<DataGridTemplateColumn> 
    <DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
      <ComboBox 
       SelectedValue="{Binding ReferencedName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
       ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, 
        Path=Data.NameList, Mode=TwoWay}" /> 
     </DataTemplate> 
    </DataGridTemplateColumn.CellTemplate> 
</DataGridTemplateColumn> 

:これまでのところ私のような何かを試してみました

System.Windows.Data Error: 40 : BindingExpression path error: 'Data' property not found on 'object' ''DataGrid' (Name='coefficientList')'. BindingExpression:Path=Data.NameList; DataItem='DataGrid' (Name='coefficientList'); target element is 'ComboBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')

誰でも正しい方向に向けることができますか?

答えて

1

Data.NameListは、DataContextオブジェクトのプロパティです。バインディングパスを変更する

Path=DataContext.Data.NameList 
+0

だから、簡単ではありません!どうもありがとうございました! – Charlie

+0

はい、しばしば、自分自身を忘れてしまい、 'RelativeSource' – ASh

関連する問題