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')
誰でも正しい方向に向けることができますか?
だから、簡単ではありません!どうもありがとうございました! – Charlie
はい、しばしば、自分自身を忘れてしまい、 'RelativeSource' – ASh