Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7433985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:45:37+00:00 2026-05-29T09:45:37+00:00

Since hours I am working on a very hard problem: How is a DataGrid

  • 0

Since hours I am working on a very hard problem:

How is a DataGrid that is bound to an ObservableCollection correctly updated when another ObservableCollection that is inside the ObservableCollection the DataGrid is bound to changes ?

So far the DataGrid onnly refreshes when i click on the corresponding cell.

I have prepared a complete source code example to illustrate the following (very simple) situation:

There is a ViewModel that holds a List. This List is an ObservableCollection and hold two things: An integer and another List (again an ObservableCollection) that holds four integers.
Then there is a DataGrid that has two columns. One column for the integer number and one column for the list of integers.
This little application has buttons to modify the integers in the nested list i.e. adding +1 to one of the four integers.
The GOAL is that the modification of the nested list is getting reflected by the DataGrid.
The PROBLEM so far is, that this only happens on a with an external trigger (e.g. a click on the corresponding cell, or a click on one column header that sorts a column etc.)

So here is the complete code:

This is the ViewModel code that the DataGrid is bound to:

public class ViewModel: INotifyPropertyChanged {

    private Items items;

    public Items Items {
        get { return items; }
        set {
            items = value;
            firePropertyChanged("Items");
        }
    }

    public ViewModel() {

        Items = new Items();
    }

    private void firePropertyChanged(string property) {

        if (PropertyChanged != null) {


            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

public class Items: ObservableCollection<Item> {

    public Items()
        : base() {

        this.CollectionChanged += new NotifyCollectionChangedEventHandler(OnCollectionChanged);
    }

    private void OnCollectionChanged(object o, NotifyCollectionChangedEventArgs e) {


        if (e.NewItems != null) {

            foreach (Object item in e.NewItems) {

                (item as INotifyPropertyChanged).PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
            }
        }

        if (e.OldItems != null) {

            foreach (Object item in e.OldItems) {

                (item as INotifyPropertyChanged).PropertyChanged -= new PropertyChangedEventHandler(item_PropertyChanged);
            }
        }
    }

    void item_PropertyChanged(object sender, PropertyChangedEventArgs e) {

        NotifyCollectionChangedEventArgs a = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
        OnCollectionChanged(a);
    }
}

public class List: ObservableCollection<NumberItem> {

    public List()
        : base() {

        this.CollectionChanged += new NotifyCollectionChangedEventHandler(OnCollectionChanged);
    }

    private void OnCollectionChanged(object o, NotifyCollectionChangedEventArgs e) {

        if (e.NewItems != null) {

            foreach (Object item in e.NewItems) {

                (item as INotifyPropertyChanged).PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
            }
        }

        if (e.OldItems != null) {

            foreach (Object item in e.OldItems) {

                (item as INotifyPropertyChanged).PropertyChanged -= new PropertyChangedEventHandler(item_PropertyChanged);
            }
        }
    }

    void item_PropertyChanged(object sender, PropertyChangedEventArgs e) {

        NotifyCollectionChangedEventArgs a = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
        OnCollectionChanged(a);
    }
}

public class NumberItem : INotifyPropertyChanged {

    private int number;

    public int Number {
        get { return number; }
        set {
            number = value;
            firePropertyChanged("Number");   
        }
    }

    public NumberItem(int i) {

        Number = i;
    }

    private void firePropertyChanged(string property) {

        if (PropertyChanged != null) {

            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

}

public class Item : INotifyPropertyChanged {

    private List list;

    public List List {
        get { return list; }
        set {
            list = value;
            firePropertyChanged("List");
        }
    }

    private int numberOne;

    public int NumberOne {
        get { return numberOne; }
        set {
            numberOne = value;
            firePropertyChanged("NumberOne");
        }
    }

    private void firePropertyChanged(string property) {

        if (PropertyChanged != null) {

            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

/// <summary>
/// This converter simply transforms the list of integers into a string.
/// </summary>
public class Converter : IValueConverter {

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {

    List l = (List)value;

    string s = "";

    return s + l[0].Number + " " + l[1].Number + " " + l[2].Number + " " + l[3].Number;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {

        return null;
    }
}

The code of the buttons that manipulate the integers of the nested list is the following:

private void plus1L(object sender, RoutedEventArgs e) {

        vm.Items[0].List[0].Number += 1;

    }

And finally this is the XAML where the DataGrid is getting bound:

<sdk:DataGrid x:Name="dg" Margin="17,139,21,0" ItemsSource="{Binding Items}" AutoGenerateColumns="False" VerticalAlignment="Top" Height="164" d:LayoutOverrides="Width, HorizontalMargin">
        <sdk:DataGrid.Columns>
            <sdk:DataGridTextColumn x:Name="A" Header="A" Binding="{Binding NumberOne}"/>
            <sdk:DataGridTextColumn x:Name="List" Header="List" Binding="{Binding List, Converter={StaticResource Converter}}"/>
        </sdk:DataGrid.Columns>
    </sdk:DataGrid>*emphasized text*
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:45:38+00:00Added an answer on May 29, 2026 at 9:45 am

    I already told you that you just need to fire change events for the List property when you change anything about it, it’s not that hard…

    Edit: In the handler you change the list in some way and you do nothing.

    private void plus1L(object sender, RoutedEventArgs e)
    {
        vm.Items[0].List[0].Number += 1;
        vm.Items[0].OnPropertyChanged("List"); // This is needed if you bind to List.
    }
    

    To be even more explicit about this: The binding does not care about anything other than the property path you bind to. Everything that happens inside the bound property is unknown to it, so you need to forward internal changes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the code that cause me problem since few hours: TabItem newTab =
I have 2 date object in the database that represent the company's working hours.
I have a very strange problem, which took already several hours of my attention:
i'm working since hours on this issue without any success, hope to get some
I have an issue that I've spent hours trying to figure out, but since
I've been working on this problem for 7 hours now, and I still have
I have a query that fetches employee's name, date, working hours, wage and calculate
Since Graduating from a very small school in 2006 with a badly shaped &
I am currently working on a project that involves crawling certain websites. However sometimes
I've been working on this for about 5 hours for homework and I was

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.