Aidenn
Aidenn

Reputation: 559

Silverlight4 + C#: Using INotifyPropertyChanged in a UserControl to notify another UserControl is not notifying

I have several User Controls in a project, and one of them retrieves items from an XML, creates objects of the type "ClassItem" and should notify the other UserControl information about those items.

I have created a class for my object (the "model" all items will have):

public class ClassItem
{
    public int Id { get; set; }
    public string Type { get; set; }
}

I have another class that is used to notify the other User Controls when an object of the type "ClassItem" is created:

public class Class2: INotifyPropertyChanged
{
    // Properties
    public ObservableCollection<ClassItem> ItemsCollection { get; internal set; }

    // Events
    public event PropertyChangedEventHandler PropertyChanged;

    // Methods
    public void ShowItems()
    {
        ItemsCollection = new ObservableCollection<ClassItem>();

        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs("ItemsCollection"));
        }
    }
}

The data comes from an XML file that is parsed in order to create the objects of type ClassItem:

void DisplayItems(string xmlContent)
    {
        XDocument xmlItems = XDocument.Parse(xmlContent);

        var items = from item in xmlItems.Descendants("item")
                    select new ClassItem{
                        Id = (int)item.Element("id"),
                        Type = (string)item.Element("type)                            
                    };

    }

If I'm not mistaken, this is supposed to parse the xml and create a ClassItem object for each item it finds in the XML. Hence, each time a new ClassItem object is created, this should fire the Notifications for all the UserControls that are "bind" to the "ItemsCollection" notifications defined in Class2.

Yet the code in Class2 doesn't even seem to be run :-( and there are no notifications of course...

Am I mistaken in any of the assumptions I've done, or am I missing something? Any help would be appreciated!

Thx!

Upvotes: 2

Views: 803

Answers (1)

Jason Rowe
Jason Rowe

Reputation: 6286

The property has to be accessed for the notification to work. I don't see any place in the code where you are setting a value to "ItemsCollection".

I usually follow this pattern:

  public ObservableCollection<ClassItem> ItemsCollection
        {
            get
            {
                return _itemsCollection;
            }
            set
            {
                _itemsCollection= value;
                NotifyPropertyChanged("ItemsCollection");
            }
        }

Then update the ItemsCollection.

    //before using the ObservableCollection instantiate it.
    ItemsCollection= new ObservableCollection<ClassItem>();

    //Then build up your data however you need to.
    var resultData = GetData();

    //Update the ObservableCollection property which will send notification
    foreach (var classItem in resultData)
    {
        ItemsCollection.Add(classItem);
    }

Upvotes: 1

Related Questions