Background
If we have a CLR object such as:
class Person
{
public string FirstName {get; set;}
public string LastName {get; set;}
}
We can create an ObservableCollection, add a little boiler plate code and voila we can bind this to a DataGrid with XAML like:
<DataGrid ItemsSource="{Binding PropertyOnPersonObject}"\>
And the DataGrid knows to take this object and populate each column automatically.
What happened
Within a viewmodel, I wanted to create a single column DataGrid with an ObservableCollection<decimal>
. So I made a public property and bound just like above. What kept happening was I'd get the correct number of rows but no data. Just using ItemsSource wasn't enough.
I finally figured out I needed this:
<DataGrid ItemsSource="{Binding PropertyOnSomething}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding}"/>
</DataGrid.Columns>
</DataGrid>
The DataGrid needed this extra binding code at the column level. Why?