Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF Databinding with object source fails to update
    text
    copied!<p>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:</p> <pre><code>public class SingleItem { private String _name=null; public String Name { get { return _name; } set { _name=value; } } public SingleItem(String name) { Name=name; } </code></pre> <p>The class ManyItems hosts the Collection and is defined like this:</p> <pre><code>public class ManyItems{ private ObservableCollection&lt;SingleItem&gt; allItems=new ObservableCollection&lt;SingleItem&gt;(); public ManyItems() { AllItems.Add(new SingleItem("inside"));//debug code } public ObservableCollection&lt;SingleItem&gt; AllItems { get { return allItems; } set { allItems=value; } } public void AddItem(SingleItem item) { AllItems.Add(item); } } </code></pre> <p>In my main window I just want to update ManyItems when the user presses a button:</p> <pre><code>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++; } </code></pre> <p>}</p> <p>Finally my XAML looks like this (shortened where "..."):</p> <pre><code>&lt;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"&gt; &lt;Window.Resources&gt; &lt;local:ManyItems x:Key="ManyItemsDataSource" d:IsDataSource="True"/&gt; &lt;/Window.Resources&gt; &lt;Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource ManyItemsDataSource}}"&gt; &lt;Button Content="Button" .... Click="Button_Click"/&gt; &lt;DataGrid ... ItemsSource="{Binding AllItems}"/&gt; &lt;/Grid&gt; </code></pre> <p></p> <p>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.</p> <p>Thanks for your help, Sebastian</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload