user1016018
user1016018

Reputation: 21

Synchronization between silverlight datagrid and its bound datasource

In Silverlight 4, I've bound a datagrid to an ObservableCollection datasource.

Here is the xaml code for the interface:

<sdk:DataGrid AutoGenerateColumns="False" Height="179" HorizontalAlignment="Left" Margin="667,10,0,0" Name="dgASupprimer" VerticalAlignment="Top" Width="334" DataContext="{Binding BindsDirectlyToSource=True, ValidatesOnNotifyDataErrors=False, Mode=OneWay}" LoadingRow="dgASupprimer_LoadingRow" />
    <Button Content="ASupprimer" Height="23" HorizontalAlignment="Left" Margin="905,205,0,0" Name="bASupprimer" VerticalAlignment="Top" Width="75" Click="bASupprimer_Click" />

And the one for initializing the datasource:

public class fmLabClass
    {
        public string Nom { get; set; }
        public int Age { get; set; }
        public fmLabClass(string nom, int age) { Age = age; Nom = nom; }
    }
    System.Collections.ObjectModel.ObservableCollection<fmLabClass> fmLabObservableCollection = new System.Collections.ObjectModel.ObservableCollection<fmLabClass>() { 
            new fmLabClass("Person1",34),
            new fmLabClass("Person2",36),
            new fmLabClass("Person3",45)
        };

When I press the bASupprimer button, I wish to change a value of an attribute on the object, and get in return the datagrid reevaluated.

private void bASupprimer_Click(object sender, RoutedEventArgs e)
    {
        dgASupprimer.SelectedIndex = 2;
        ((fmLabClass)(dgASupprimer.SelectedItem)).Age++;
    }

The current result is that the datagrid doesn’t refresh automatically. How can I do that?

Thx

Upvotes: 2

Views: 357

Answers (3)

user1029447
user1029447

Reputation:

You can also watch this sample chapter on Databinding from Billy Hollis:

http://s3.amazonnaws.com/dnrtv/dnrtv_0175.wmv

Not in French though.

Upvotes: 0

iCollect.it Ltd
iCollect.it Ltd

Reputation: 93631

You need to make sure your properties are notify properties if you want them to broadcast changes. Full required changes shown below. This is a common pattern you will need to know about in Silverlight if you want binding to work. There are code snippets about for properties like these (to save typing).

    public class fmLabClass : INotifyPropertyChanged
    {
        private string _Nom;
        public string Nom 
        { 
            get {return _Nom;} 
            set
            {
                if (_Nom != value)
                {
                    _Nom = Value; 
                    OnPropertyChanged("Nom");
                }
            }
        }

        private int _Age;
        public int Age
        { 
            get {return _Age;} 
            set
            {
                if (_Age!= value)
                {
                    _Age= Value; 
                    OnPropertyChanged("Age");
                }
            }
        }

        public fmLabClass(string nom, int age) { Age = age; Nom = nom; }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyname)
        {
            if (PropertyChanged != null)
            {
                 PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
            }
        }
    }

Upvotes: 1

Justin XL
Justin XL

Reputation: 39006

Your fmLabClass needs to implement INotifyPropertyChanged in order to tell the UI that there is a change in your object.

Upvotes: 0

Related Questions