Click here to Skip to main content
16,018,057 members
Home / Discussions / WPF
   

WPF

 
AnswerRe: incorrect value retrieved. Pin
Abhinav S11-May-11 6:27
Abhinav S11-May-11 6:27 
QuestionUse of Observablecollection in Silverlight Pin
Member 455049311-May-11 3:17
Member 455049311-May-11 3:17 
AnswerRe: Use of Observablecollection in Silverlight Pin
Ian Shlasko11-May-11 3:27
Ian Shlasko11-May-11 3:27 
GeneralRe: Use of Observablecollection in Silverlight Pin
Member 455049311-May-11 4:56
Member 455049311-May-11 4:56 
GeneralRe: Use of Observablecollection in Silverlight Pin
Ian Shlasko11-May-11 5:11
Ian Shlasko11-May-11 5:11 
GeneralRe: Use of Observablecollection in Silverlight Pin
Member 455049311-May-11 5:42
Member 455049311-May-11 5:42 
GeneralRe: Use of Observablecollection in Silverlight Pin
Ian Shlasko11-May-11 6:03
Ian Shlasko11-May-11 6:03 
GeneralRe: Use of Observablecollection in Silverlight Pin
Pete O'Hanlon11-May-11 6:31
mvePete O'Hanlon11-May-11 6:31 
There are two parts to binding that you are concerned with here. The first, as you are aware, is the use of ObservableCollection. This tells you that the collection has changed; in other words that items have been added or deleted in the collection. This does not tell you that an individual item has changed.

To capture notifications that an individual item has changed, you need to implement INotifyPropertyChanged in the class (or an ancestor of). Simplistically, when an item changes, the PropertyChanged event fires, and this tells the binding engine that an item needs to be updated in the UI. If you look at samples, you'll typically see a class called something like ViewModelBase. This class is the one that implements the property changed notification, saving you from having to update the code in lots and lots of locations. Here's a typical example of this class:
C#
public abstract class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    /// <summary>
    /// Raised because the property has changed.
    /// </summary>
    /// <param name="property">The name of the property that changed.</param>
    protected virtual void OnChanged(string property)
    {
      var handler = PropertyChanged;
      if (handler == null)
      {
        return;
      }

      handler(this, new PropertyChangedEventArgs(property));
    }
}
Now, you'll derive from this class and call OnChanged in your property setters. This would typically look something like this:
C#
private string _passCode;
public string PassCode
{
  get { return _passCode; }
  set
  {
    if (_passCode == value) return;
    _passCode = value;
    OnChanged("PassCode");
  }
}
When the user changes a value in PassCode, the value is updated and then the change notification is raised which tells the binder to update only that named field (if you want to update all fields, you pass an empty string to OnChanged). There are a couple of things to note here - always test to see if the update value is the same as the value you already store, this prevents you updating something that hasn't changed; the name of the property that is raised must match the name of the property, so calling "Pa4sCode" would fail to update the value as it's not the name of the property (PassCode).

I hope that this makes sense to you. All too often people fall into the trap of assuming that ObservableCollection covers both sides. It's an easy mistake to make.

Forgive your enemies - it messes with their heads


My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility


GeneralRe: Use of Observablecollection in Silverlight Pin
Member 455049312-May-11 6:01
Member 455049312-May-11 6:01 
GeneralRe: Use of Observablecollection in Silverlight Pin
Pete O'Hanlon12-May-11 6:21
mvePete O'Hanlon12-May-11 6:21 
QuestionDataGrid - CheckBox selection from Code. Pin
Nanda_MR10-May-11 18:39
Nanda_MR10-May-11 18:39 
AnswerRe: DataGrid - CheckBox selection from Code. Pin
Pete O'Hanlon10-May-11 19:29
mvePete O'Hanlon10-May-11 19:29 
QuestionOverriding the default keys used to navigate in RadioButtons [WPF] [VB] Pin
Jayme6510-May-11 11:07
Jayme6510-May-11 11:07 
AnswerRe: Overriding the default keys used to navigate in RadioButtons [WPF] [VB] Pin
SledgeHammer0110-May-11 11:55
SledgeHammer0110-May-11 11:55 
GeneralRe: Overriding the default keys used to navigate in RadioButtons [WPF] [VB] Pin
Jayme6510-May-11 12:58
Jayme6510-May-11 12:58 
AnswerRe: Overriding the default keys used to navigate in RadioButtons [WPF] [VB] Pin
Mark Salsbery10-May-11 12:04
Mark Salsbery10-May-11 12:04 
QuestionProblem in VisualTreeHelper.GetParent. Please guide me Pin
Hema Bairavan10-May-11 6:44
Hema Bairavan10-May-11 6:44 
AnswerRe: Problem in VisualTreeHelper.GetParent. Please guide me [modified] Pin
Mark Salsbery10-May-11 8:46
Mark Salsbery10-May-11 8:46 
GeneralRe: Problem in VisualTreeHelper.GetParent. Please guide me Pin
Hema Bairavan10-May-11 22:57
Hema Bairavan10-May-11 22:57 
QuestionRe: Problem in VisualTreeHelper.GetParent. Please guide me Pin
Mark Salsbery11-May-11 4:49
Mark Salsbery11-May-11 4:49 
QuestionWPF Datagrid and MVVM - Binding CellStyle to a ViewModel Property Pin
Nicolai Schrade10-May-11 5:01
Nicolai Schrade10-May-11 5:01 
AnswerRe: WPF Datagrid and MVVM - Binding CellStyle to a ViewModel Property Pin
Ian Shlasko10-May-11 5:38
Ian Shlasko10-May-11 5:38 
GeneralRe: WPF Datagrid and MVVM - Binding CellStyle to a ViewModel Property Pin
Nicolai Schrade10-May-11 6:00
Nicolai Schrade10-May-11 6:00 
GeneralRe: WPF Datagrid and MVVM - Binding CellStyle to a ViewModel Property Pin
Ian Shlasko10-May-11 6:09
Ian Shlasko10-May-11 6:09 
GeneralRe: WPF Datagrid and MVVM - Binding CellStyle to a ViewModel Property Pin
Nicolai Schrade10-May-11 6:35
Nicolai Schrade10-May-11 6:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.