Binding a nested object to a DataGrid in c# WPF -


i trying bind data displayed in datagrid dynamic list of object (whispermodel) inside object(whisperreader). datagrid displays headers, no values. how can make datagrid dynamically update when list "whispers" changed?

main window xaml:

<datagrid x:name="whisperdatagrid" margin="10,69,10,10" isreadonly="true" itemssource="{binding}"/> 

main window c#

public partial class mainwindow : window {     private whisperreader wr;     public mainwindow()     {         initializecomponent();         wr = new whisperreader();         whisperdatagrid.datacontext = wr.whispers;     } 

whisperreader:

class whisperreader {     public observablecollection<whispermodel> whispers { get; private set; }      public whisperreader()     {         whispers = new observablecollection<whispermodel>();     } 

whispermodel:

class whispermodel {     public datetime senttime { get; set; }     public string sender { get; set; }     public string message { get; set; } } 

i think problem doesn't know when update because:

  1. you have made whispers list data context.
  2. the properties binding don't use inotifypropertychanged.
  3. whisperreader , whispermodel not public

all bindings must public, must properties, , must call propertychanged method. propertychanged function triggers binding updates.

try this...

public partial class mainwindow : window {     private whisperreader wr;     public mainwindow()     {         initializecomponent();         wr = new whisperreader();         whisperdatagrid.datacontext = wr;     }  public class whisperreader : inotifypropertychanged {     observablecollection<whispermodel> _whispers;     public observablecollection<whispermodel> whispers      {        { return _whispers; }        private set        {          _whispers = value;          notifypropertychanged();        }     }      public whisperreader()     {         whispers = new observablecollection<whispermodel>();     }      public event propertychangedeventhandler propertychanged;     private void notifypropertychanged([callermembername] string propertyname = "")     {         if (propertychanged != null)         {             propertychanged(this, new propertychangedeventargs(propertyname));         }     }   public class whispermodel : inotifypropertychanged {     public datetime senttime { get; set; }      private string _sender;     public string sender      {          { return _sender; }          set { _sender = value; notifypropertychanged();     }      private string _message;     public string message      {          { return _message; }          set { _message = value; notifypropertychanged();     }      public event propertychangedeventhandler propertychanged;     private void notifypropertychanged([callermembername] string propertyname = "")     {         if (propertychanged != null)         {             propertychanged(this, new propertychangedeventargs(propertyname));         }     } }  <datagrid x:name="whisperdatagrid" margin="10,69,10,10" isreadonly="true" autogeneratecolumns="true" itemssource="{binding whispers}"/> 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -