Bob Tway
Bob Tway

Reputation: 9613

Watching changes in children of bound properties

I have a data object that looks a lot like this:

public class ItemSetting
{
    public string Description { get; set; }
    public string Setting { get; set; }
}

One of my ViewModels has an ObservableCollection of such objects in a property called ItemSettings, which it binds to a ListView, like this:

<ListView  ItemsSource="{Binding ItemSettings, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Description">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Description}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>

            <GridViewColumn Header="Setting">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Setting}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

And this works fine. The list displays, gets updated when items are added or removed and if the user makes changes to the Description or Setting values, they are reflected in the underlying objects and can be sent back to the database when the users press the "save" button.

The list is often quite large, and the users have asked me to add a feature to tell them if there are unsaved changes in the list to reduce the risk of losing work. Elsewhere in the application we've done this via an UnsavedChanges boolean property in the base ViewModel which we can check before closing and that works fine.

The trouble is that I don't know how I can pick up on the event of a user typing into a Setting or Description box in order to set the boolean. ItemSettings exposes an ObservableCollection and I can detect when that changes, of course, via the set part of the property. But that doesn't trip when child values are changed.

Is there a way I can do this without pulling Description and Setting out into individual properties and using the set values there?

Upvotes: 0

Views: 156

Answers (1)

Nantharupan
Nantharupan

Reputation: 614

The class ItemSetting should implements the INotifyPropertyChanged interface in order to detect the changes,

public class ItemSetting : ViewModelBase
{
    public string Description 
     get { return _description; } 
     set { _description = value; 
         this.RaisePropertyChanged("Description");
         }       
}

Upvotes: 1

Related Questions