Architect's Log

I'm a Cloud Architect. I'm highly motivated to reduce toils with driving DevOps.

ObservableCollectionでリストへの追加を通知する

アプリ実行

起動


入力


追加


再度追加


ソースコード

App.xaml
<Application x:Class="HelloWorld.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
</Application>
MainWindow.xaml
<Window x:Class="HelloWorld.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="250" Width="200">
    <StackPanel>
        <StackPanel.Resources>
            <DataTemplate x:Key="addressTemplate">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Path=Zip}" />
                    <TextBlock Text="{Binding Path=City}" />
                </StackPanel>
            </DataTemplate>
        </StackPanel.Resources>
        <TextBlock Text="{Binding Path=First}" />
        <TextBlock Text="{Binding Path=Last}" />
        <TextBlock Margin="5" FontSize="14pt">住所</TextBlock>
        <ListBox
            ItemsSource="{Binding Path=Addresses}"
            ItemTemplate="{DynamicResource addressTemplate}" />
        <TextBlock Margin="5" FontSize="14pt">新しい住所を追加</TextBlock>
        <TextBox Name="zip" />
        <TextBox Name="city" />
        <Button Click="Button_Click">追加</Button>
    </StackPanel>
</Window>
MainWindow.xaml.cs
using System.Windows;

namespace HelloWorld {
    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();

            DataContext = new Person() {
                First = "山田",
                Last = "太郎"
            };
        }

        private void Button_Click(object sender, RoutedEventArgs e) {
            Address a = new Address() {
                Zip = zip.Text,
                City = city.Text
            };

            ((Person) DataContext).Addresses.Add(a);
        }
    }
}
Person.cs
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace HelloWorld {
    public class Person : INotifyPropertyChanged {

        IList<Address> addresses = new ObservableCollection<Address>();

        public IList<Address> Addresses {
            get { return addresses; }
        }
        
        private string first;

        public string First {
            get {
                return first;
            }
            set {
                first = value;
                OnPropertyChanged("First");
            }
        }

        private string last;

        public string Last {
            get {
                return last;
            }
            set {
                last = value;
                OnPropertyChanged("Last");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

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

        }
    }
}
Address.cs
namespace HelloWorld {
    public class Address {
        public string Zip { get; set; }
        public string City { get; set; }
    }
}

参考

ObservableCollection(T) クラス (System.Collections.ObjectModel)
項目が追加、削除されたとき、またはリスト全体が更新されたときに通知を提供する動的なデータ コレクションを表します。