Reputation: 664
I have a DataGrid
bound to a List<Coefficients>
in memory. The Coefficients
class has a string Name
as ID, and a lot of other fields (some names and identifying details have been changed to protect the privacy of the code). The DataGrid
code is like this:
<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>
Now the elements in the datagrid must reference another element in the same collection, so I have a new field in the Coefficients
class: string ReferencedName
. This is the string ID of the referenced element in the collection. So I would like to have a new column in the datagrid to select the value of the referenced element via a combobox, and that combobox needs to be populated with the Name
column of the same datagrid.
How can I achieve that? So far I have tried something like:
<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>
where NameList
is
public IEnumerable<string> NameList
{
get
{
return CoefficientList.Select(c => c.Name);
}
}
but anything I have tried so far always gives a combobox with no options, and errors in the output window like
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')
Can anyone point me in the right direction?
Upvotes: 0
Views: 294
Reputation: 35646
Data.NameList
is a property of DataContext
object. change binding path
Path=DataContext.Data.NameList
Upvotes: 1