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 974115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:22:58+00:00 2026-05-16T03:22:58+00:00

I have been struggling a while with this problem and read a lot but

  • 0

I have been struggling a while with this problem and read a lot but most of the examples are too simple. I am trying to bind a very simple ObservableCollection to a DataGrid.
The super simple objects within the Collection are “SingleItems” which are defined like this:

public class SingleItem {
private String _name=null;

public String Name {
  get { return _name; }
  set { _name=value; }
}

public SingleItem(String name) {
  Name=name;
}

The class ManyItems hosts the Collection and is defined like this:

public class ManyItems{
private ObservableCollection<SingleItem> allItems=new ObservableCollection<SingleItem>();


public ManyItems() {
  AllItems.Add(new SingleItem("inside"));//debug code
}


public ObservableCollection<SingleItem> AllItems {
  get { return allItems; }
  set { allItems=value; }
}

public void AddItem(SingleItem item) {
  AllItems.Add(item);
}
}

In my main window I just want to update ManyItems when the user presses a button:

public partial class MainWindow : Window{
int count=0;
ManyItems _items=new ManyItems();

public ManyItems Items {
  get { return _items; }
  set { _items=value; }
}

public MainWindow(){
this.InitializeComponent();
}

private void Button_Click(object sender, System.Windows.RoutedEventArgs e){
Items.AddItem(new SingleItem("name_"+count));
count++;
}

}

Finally my XAML looks like this (shortened where “…”):

<Window
...
xmlns:local="clr-namespace:DataGridTEst" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:XamlGeneratedNamespace="clr-namespace:XamlGeneratedNamespace" mc:Ignorable="d"
x:Class="DataGridTEst.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="640" Height="480">

<Window.Resources>
    <local:ManyItems x:Key="ManyItemsDataSource" d:IsDataSource="True"/>
</Window.Resources>

<Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource ManyItemsDataSource}}">
    <Button Content="Button" .... Click="Button_Click"/>
    <DataGrid ... ItemsSource="{Binding AllItems}"/>
</Grid>

When I run this app, the grid shows the SingleItem “inside” which I created in the constructor. However no changes to the underlying Collections are reflected in the datagrid. I tried also to use INotifyPropertyChanged but without success. I think I have a grave error in my understanding.
Can anybody explain me what I am doing wrong? Also online examples (where not everything is done inside constructors) are greatly appreciated.

Thanks for your help,
Sebastian

  • 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-16T03:22:59+00:00Added an answer on May 16, 2026 at 3:22 am

    The instance of ManyItems that you’re modifying in the code behind is not the same instance that your XAML is bound to. The <local:ManyItems/> in your Window‘s Resources is causing a separate, distinct instance of ManyItems to be created.

    Use the same instance, and it will work fine:

    public MainWindow(){
        this.InitializeComponent();
        this.DataContext = _items;
    }
    

    And in the XAML remove the Resources section and change your Grid to simply:

    <Grid x:Name="LayoutRoot">
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this is a very simple question but I have been struggling with
I have been struggling with this problem for a while now, so any help
I have been struggling with this problem for a while now. I have a
I might be missing something, but I have been struggling with this problem for
I have been struggling with this for a while now. given a set of
I've been struggling with this issue for a while now. I have OpenCart 1.5.2.1
I've been struggling with this for a while. Basically, I want to have two
I have been struggling all afternoon trying to get a simple mailto: tag to
I have been struggling with this seeminly easy problem for 48 hours, and I
I have been struggling with a unit testing problem on Android for a while

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.