Khoa Vo
Khoa Vo

Reputation: 352

WPF: What would happen List<T> instead of ObservableCollection<T> for data binding?

I am experimenting with data binding using this code here:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace KhoaOOP6
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        // LOOK AT THIS!!! This is the data; if you use only List the application will break
        ObservableCollection<Student> studentGroup = new ObservableCollection<Student>()
        {
             new Student("Jaakko Laaksonen", "Sammonkatu 5", new DateTime(1995,11,23)),
             new Student("Kaarina Helin", "Tullikatu 7", new DateTime(1970,01,09)),
             new Student("Maria Nyman", "Adlercreutzinkatu   33", new DateTime(1980,02,24)),
             new Student("Liisa Danielsson", "Askolinintie 5", new DateTime(1981,05,26)),
             new Student("Aino Henriksson", "Caloniuksenkatu 12", new DateTime(1971,10,07)),
             new Student("Elsi Gustafsson", "Haukantie 21", new DateTime(1972,11,11)),
             new Student("Henri Mattsson", "Jakarinkatu 18", new DateTime(2003,12,17)),
             new Student("Emili Ojala", "Karhupolku 29", new DateTime(1967,04,30)),
             new Student("Sinikka Jokinen", "Kolavuorenkuja 32", new DateTime(1969,08,15)),
             new Student("Aurora Niemi", "Laamanninpolku 39", new DateTime(1989,09,20))
        };

        public MainWindow()
        {
            InitializeComponent();

            // Do our binding business
            // Create a new binding
            Binding myNewBindDef = new Binding()
            {
                Mode = BindingMode.OneWay,
                Source = studentGroup
            };

            // LOOK AT THIS!!!!!!!!!!!!!!!
            BindingOperations.SetBinding(studentGrid, DataGrid.ItemsSourceProperty, myNewBindDef);
        }

        private void AddNewBtn_Click(object sender, RoutedEventArgs e)
        {
            // Create a new Student:
            Student newStudent = new Student(NameTxtBox.Text, AddressTxtBox.Text, DateTime.Parse(DobTxtBox.Text));

            //Add to database
            studentGroup.Add(newStudent);
        }
    }
}

It works fine for now, BUT there are some things I don't understand:

*Edit: Sorry I didn't say this more clearly: Visual Studio did not tell me anything about the exception that occurred, I thought Debugging would do something but it didn't crash so...

Upvotes: 1

Views: 142

Answers (1)

Rich Bryant
Rich Bryant

Reputation: 907

This is kind of the point of databinding. Observables are so-called because they implement INotifyPropertyChanged which either has code associated with it to perform UI updates or the framework you're using does that for you.

A list is not - by default - observable and so there's no plumbing in place to update the UI when the contents of the list change.

Of course, there's absolutely nothing to stop you from implementing a new List<T> type which does implement INotifyPropertyChanged except that you're reinventing the wheel since that's exactly what an ObservableCollection<T> is.

Problem #1: If I used (List studentGroup) instead of (ObservalbeCollection studentGroup), the UI initially displays the list correctly. However, when I modified the studentGroup list, the application suddenly crashes. Why?

The UI initially displays the list correctly because it has bound to the static List<T> collection. If you modify the StudentGroup list, the application will crash because the List you bound to no longer exists - rather a modified object exists and has not notified the UI that it is now the binding target.

Problem #2: Even weirder than that, the application only crashed in Release mode, when I switched to Debug mode to track down the error, it doesn't crash. Why? What if I hadn't known beforehand about ObservableCollection, how would I track down bugs like these in the future

It might not have crashed but I'll be the UI didn't update. If you hadn't known about ObservableCollection then you would not be qualified to work with WPF databinding.

Upvotes: 1

Related Questions