I am writing a WPF program using C# and targeting .Net 4.0. I am trying to follow the MVVM pattern, where my view (code-behind) has minimal-to-no code.
I have a List<MyRecord>
that I want to display in the GUI using a DataGrid
. In my XAML, I have the following:
<DataGrid x:Name="RecordGrid" ...>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding RecId}" Header="Record ID"/>
<DataGridTextColumn Binding="{Binding Name}" Header="Name"/>
</DataGrid.Columns>
</DataGrid>
All that's left is to bind this grid to a data collection.
When I bind it in the code-behind file, it works fine:
RecordGrid.ItemsSource = MyRecordList;
However, I would prefer to bind it declaratively in XAML. So I tried this:
<DataGrid x:Name="RecordGrid" ItemsSource="{Binding MyRecordList}" ...>
but it silently doesn't work. There is no XAML binding error message when the datagrid loads. I set a breakpoint on MyRecordList's get
method, and it's never invoked as long as ItemsSource is defined declaratively.
How can I get my datagrid to pull from MyRecordList
via XAML?