Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to bind datagrid to collectionviewsource using xaml

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..

like image 293
TTT Avatar asked Dec 01 '14 11:12

TTT


1 Answers

To avoid your code behind approach you should use the MVVM-pattern MVVM Model View ViewModel . A possible solution could be a "Person" (acting as the Model) like this:

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

The you could implement a ViewModel initializing a property with an ObservableCollection of Persons.

public class ViewModel
{
    public ObservableCollection<Person> Persons { get; set; }

    public ViewModel()
    {
        Persons = new ObservableCollection<Person>();
    }
}

Your MainWindow.cs now has to initialize the ViewModel:

public partial class MainWindow : Window
{
    public ViewModel ViewModel;

    public MainWindow()
    {
        ViewModel = new ViewModel();

        ViewModel.Persons.Add(new Person
        {
            Age = 29,
            Name = "Mustermann"
        });

        ViewModel.Persons.Add(new Person
        {
            Age = 35,
            Name = "Meyer"
        });

        this.DataContext = ViewModel;

        InitializeComponent();
    }

It is important to set the DataContext to the ViewModel object. I added a Button an the method to add a Person.

    private void AddPersonOnClick(object sender, RoutedEventArgs e)
    {
        ViewModel.Persons.Add(new Person
        {
            Age = 55,
            Name = "Sand"
        });
    }

Now you can instantiate an CollectionViewSource in XAML and bind it to the Persons ObservableCollection property in your ViewModel.

<Window x:Class="DataGridStackoverflow.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <CollectionViewSource x:Key="PersonsCollectionViewSource" Source="{Binding Persons}" />
</Window.Resources>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <DataGrid Grid.Row="0" ItemsSource="{Binding Source={StaticResource PersonsCollectionViewSource}}" />
    <Button x:Name="AddPerson" Grid.Row="1" Click="AddPersonOnClick" HorizontalAlignment="Left">Add Person</Button>
</Grid>

Finally you have to set the ItemsSource as you posted it to the CollectionViewSource and it works like a charm.

EDIT

I tried your solution and it's working as well. MainWindow.xaml:

    <Window.Resources>
    <dataGridStackoverflow:Persons x:Key="Persons" />
    <CollectionViewSource x:Key="PersonsCollectionViewSource" Source="{StaticResource Persons}" />
</Window.Resources>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <DataGrid Grid.Row="0" ItemsSource="{Binding Source={StaticResource PersonsCollectionViewSource}}" />
    <Button x:Name="AddPerson" Grid.Row="1" Click="AddPersonOnClick" HorizontalAlignment="Left">Add Person</Button>
</Grid>

And it's important that you initialize your Persons Collection after the InitializeComponent(). MainWindow.cs

        InitializeComponent();
        Persons persons = (Persons)this.FindResource("Persons");

        persons.Add(new Person
        {
            Age = 23,
            Name = "Dude"
        });

This solution works without code-behind constructs to set the ItemsSource.

like image 70
SSchuette Avatar answered Sep 28 '22 08:09

SSchuette