views:

143

answers:

1

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!

+1  A: 

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);
    }
Jason Rowe
Jason's dead-on. Simply creating a ClassItem is not enough. You need to add each one (or the "var items" collection in DisplayItems) to the ItemsCollection of a Class2 object.
Tim Trout
Ook, I've been messing up with the code for several hours but I really can't make it work... I'm really having a hard time creating the type "GetData()" from Jason's code. I don't have such an object, I was working with the ClassItem (that provides the model) and the Class2, that should notify when the objects ClassItem are created.I tried to move part of the code that retrieves the data from the XML inside an object type GetData, but I'm not retrieving any "ClassItems" in the resultData = new GetData();I'm sure I'm missing some stupid thing...
Aidenn
It's just a function to go get your data. You would use the results of your Linq statement in your question.
Jason Rowe