简体   繁体   中英

Binding a nested object to a DataGrid in c# WPF

I am trying to bind the data displayed in a DataGrid to a dynamic list of object (WhisperModel) which is inside another object(WhisperReader). The DataGrid only displays the headers, but no values. How can I make the DataGrid dynamically update itself when the list "whispers" is 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 your problem is that it doesn't know when to update itself because:

  1. You have made the whispers list the data context.
  2. The properties that you are binding to don't use INotifyPropertyChanged.
  3. WhisperReader and WhisperModel are not public

All bindings must be public, must be properties, and must call the PropertyChanged method. The PropertyChanged function triggers the 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 
    { 
      get { 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 
    { 
        get { return _sender; } 
        set { _sender = value; NotifyPropertyChanged();
    }

    private string _message;
    public string message 
    { 
        get { 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}"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM