I have a datagrid which is bound to a collectionviewsource, which is bound to an observablecollection. In following a guide I've set it up like so:
My Persons class:
public class Persons : ObservableCollection<Person>
{
//...
}
The xaml data bindings:
<Window.Resources>
<local:Persons x:Key="_Persons"/>
<CollectionViewSource x:Key="cvsPersons" Source="{StaticResource _Persons}" />
</Window.Resources>
The datagrid binding:
<DataGrid x:Name="myDataGrid" ItemsSource="{Binding Source={StaticResource cvsPersons}}"/>
The code behind:
_Persons = (Persons)this.Resources["_Persons"];
_persons = //some method to fill perons;
cvsPersons = (CollectionViewSource)this.Resources["cvsPersons"];
cvsPersons.Source = _Persons;
The above works. My question is, why do I need to set the collectionviewsource.source in code behind using cvsPersons.Source = _Persons;? I thought the xaml in my very first snippet did this job:
_cvsPersons.Source = _Persons;
If I need all this code behind then the xaml databinding code seems of little benefit, I may as well do everything in code behind. From my (perhaps little) understanding, the only code needed in code behind would be to reference the instances setup by xaml, ie:
_Persons = (Persons)this.Resources["_Persons"];
_persons = //some method to fill perons;
cvsPersons = (CollectionViewSource)this.Resources["cvsPersons"];
If I don't have _cvsPersons.Source = _Persons; then my datagrid does not get populated. My xaml as it stands doesn't do the job. I guess my question is more concept related..