Linda
Linda

Reputation: 221

WPF DataGrid binding to List<Type>

I'm trying to bind a WPF DataGrid to a List<ClassName>.

Below is my DataGrid:

<DataGrid ItemsSource="{Binding Source=FileProcessing}" AutoGenerateColumns="True"></DataGrid>

Below I am binding the list with database records:

FileProcessing =  GetFileProcessingInfo(dtDateStart, dtDateEnd);

The FileProcessing is defined as a property below:

public List<FileProcessing_T> FileProcessing { get; set; }

The GetFileProcessingInfo Method also returns a List<FileProcessing_T> object.

The FileProcessing list does get some records from the database but the grid does not bind the data from the list.

I will appreciate your help.

Upvotes: 1

Views: 251

Answers (1)

Martin
Martin

Reputation: 5623

You can keep your databinding.

But your have to implement the INotfifyPropertyChanged interface in the class where the FileProcessing property is located. Because in the setter of FileProcessing you have to perform the change notification.

    public ObservableCollection<FileProcessing_T> FileProcessing 
    {
        get
        {
            return _fileProcessing;
        }
        set
        {
            if (_fileProcessing != value)
            {
                _fileProcessing = value;
                RaisePropertyChanged("FileProcessing");
            }
        }
    }
    ObservableCollection<FileProcessing_T> _fileProcessing;


    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    public void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

Otherwise the UI control will not know (not be notified) that the bound data has changed.


This will be enough to fix your problem. It would even work if you continued to use List<FileProcessing_T> instead of ObservableCollection<FileProcessing_T>, however the ObservableCollection also supports change notifications if single elements are added and removed from the collection while List does not.

Upvotes: 1

Related Questions