using System; using System.Collections.ObjectModel; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Collections.Specialized; namespace MyDataGrid { public partial class Page : UserControl { private BooksCollection myBooks = new BooksCollection(); public Page() { InitializeComponent(); } private void UserControl_Loaded(object sender, RoutedEventArgs e) { myDataGrid.ItemsSource = myBooks; myBooks.Add(new Book("XAMLプログラミング", "川西 裕幸", "9784797339161")); } private void Button_Click(object sender, RoutedEventArgs e) { myBooks.Add(new Book(myTitle.Text, myAuthor.Text, myISBN.Text)); } } public class BooksCollection : Collection, INotifyCollectionChanged { public new void Add(Book b) { base.Add(b); OnCollectionChanged(NotifyCollectionChangedAction.Add, b, this.IndexOf(b)); } #region INotifyCollectionChanged Member public event NotifyCollectionChangedEventHandler CollectionChanged; protected void OnCollectionChanged(NotifyCollectionChangedAction action, Book b, int index) { NotifyCollectionChangedEventHandler handler = CollectionChanged; if (handler != null) handler(this, new NotifyCollectionChangedEventArgs(action, b, index)); } #endregion } public class Book { public string Title { get; set; } public string Author { get; set; } public string ISBN { get; set; } public Book(string title, string author, string isbn) { Title = title; Author = author; ISBN = isbn; } } }