1
votes

I've got an ObservableCollection "AnswerPossibilities" that I can successfully bind a ListBox to:

<ListBox ItemsSource="{Binding AnswerPossibilities}" DisplayMemberPath="Text" />

Now, I've followed this to add columns to a DataGrid at runtime, and I'm trying to bind the same ObservableCollection "AnswerPossibilities" to a column:

DataGridTextColumn column = new DataGridTextColumn()
{
    Header = "Header 1",
    Binding = new System.Windows.Data.Binding("AnswerPossibilities")
};
ColumnCollection.Add(column);

The Grid displays "Header 1", but the "AnswerPossibilities" are missing, the grid is empty.

The XAML for the DataGrid is

<DataGrid Name="dataGrid"
local:DataGridColumnsBehavior.BindableColumns="{Binding ColumnCollection}"
AutoGenerateColumns="False"/>

I've tried adding DisplayMemberPath="Text" to the DataGrid, but that did not change a thing. After half a day with the grid.. I'd be very thankful for any help!

1
Maybe you want a DataGridComboBoxColumn with ItemsSource binded to the collection? And where do you get DataGridColumnsBehavior from? - Yoh Deadfall
Have you ensured your database connection string is correct? - Angela Marie-Daley
Hi Angela, when bound to the ListBox, I see all "AnswerPossibilities". So data is definitly there.. - peter
Yoh, I get DataGridColumnsBehavior from the link in my original post. Changing to DataGridComboBoxColumn & ItemsSource had no effect :o( - peter

1 Answers

1
votes

It looks like AnswerPossibilities is your collection that you're binding your ListBox / DataGrid to. When creating the Binding for the column you need to specify the property of the item in the ObservableCollection that you want to bind to.

Try changing as follows:

DataGridTextColumn column = new DataGridTextColumn()
{
    Header = "Header 1",
    Binding = new System.Windows.Data.Binding("Text")
};
ColumnCollection.Add(column);