1

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!

Community
  • 1
  • 1
peter
  • 2,103
  • 7
  • 25
  • 51
  • Maybe you want a `DataGridComboBoxColumn` with `ItemsSource` binded to the collection? And where do you get `DataGridColumnsBehavior` from? – Yoh Deadfall Mar 10 '15 at 16:16
  • Have you ensured your database connection string is correct? – Angela Marie-Daley Mar 10 '15 at 18:24
  • Hi Angela, when bound to the ListBox, I see all "AnswerPossibilities". So data is definitly there.. – peter Mar 10 '15 at 19:25
  • Yoh, I get DataGridColumnsBehavior from the link in my original post. Changing to DataGridComboBoxColumn & ItemsSource had no effect :o( – peter Mar 10 '15 at 19:28

1 Answers1

1

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);
KornMuffin
  • 2,887
  • 3
  • 32
  • 48
  • 1
    Hi KornMuffin, yes, AnswerPossibilities is the collection I'd like to use as binding source and yes, "Text" is the Property of an "AnswerPossibility" that I'd like to display. With your code, there is no reference to AnswerPossibilities at all? – peter Mar 10 '15 at 19:29
  • You'll need to set the DataGrid's ItemsSource="{Binding AnswerPossibilities}". – KornMuffin Mar 10 '15 at 19:31