CollectionBinding
이 예제는 ObservableCollection<T>에서 파생 되는 항목이 추가 되거나 제거 될 때 알림을 제공 하는 컬렉션 클래스 만들고 컬렉션에 바인딩하는 방법을 보여 줍니다.
컬렉션 구현
1: public class Person : INotifyPropertyChanged
2: {
3: private string _firstname;
4: private string _lastname;
5: private string _hometown;
6: public string LastName
7: {
8: get { return _lastname; }
9: set
10: {
11: if (_lastname != value)
12: {
13: _lastname = value;
14: OnPropertyChanged("LastName");
15: }
16: }
17: }
18: public string FirstName
19: {
20: get { return _firstname; }
21: set
22: {
23: if (_firstname != value)
24: {
25: _firstname = value;
26: OnPropertyChanged("FirstName");
27: }
28: }
29: }
30: public string HomeTown
31: {
32: get { return _hometown; }
33: set
34: {
35: if (_hometown != value)
36: {
37: _hometown = value;
38: OnPropertyChanged("HomeTown");
39: }
40: }
41: }
42: public Person(string first, string last, string town)
43: {
44: _firstname = first;
45: _lastname = last;
46: _hometown = town;
47: }
48: public event PropertyChangedEventHandler PropertyChanged;
49: protected void OnPropertyChanged(string name)
50: {
51: var handler = PropertyChanged;
52: handler?.Invoke(this, new PropertyChangedEventArgs(name));
53: }
54: public override string ToString()
55: {
56: return _firstname;
57: }
58: }
59: public class People : ObservableCollection<Person>
60: {
61: public People()
62: {
63: Add(new Person("Michael", "Alexander", "Bellevue"));
64: Add(new Person("Jeff", "Hay", "Redmond"));
65: Add(new Person("Christina", "Lee", "Kirkland"));
66: Add(new Person("Samantha", "Smith", "Seattle"));
67: }
68: }
컬렉션 정의
XAML에서 컬렉션을 인스턴스화하고 컬렉션을 리소스로 지정합니다.
1: <Window.Resources>
2: <local:People x:Key="MyFriends" />
3: <Style TargetType="ListBoxItem">
4: <Setter Property="FontFamily" Value="Verdana" />
5: <Setter Property="FontSize" Value="11" />
6: <Setter Property="Padding" Value="10" />
7: </Style>
8: <DataTemplate x:Key="DetailTemplate">
9: <Border Width="300" Height="100" Margin="20" BorderBrush="Aqua" BorderThickness="1" Padding="8">
10: <Grid>
11: <Grid.RowDefinitions>
12: <RowDefinition />
13: <RowDefinition />
14: <RowDefinition />
15: </Grid.RowDefinitions>
16: <Grid.ColumnDefinitions>
17: <ColumnDefinition />
18: <ColumnDefinition />
19: </Grid.ColumnDefinitions>
20: <TextBlock Grid.Row="0" Grid.Column="0" Text="First Name:"/>
21: <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=FirstName}"/>
22: <TextBlock Grid.Row="1" Grid.Column="0" Text="Last Name:"/>
23: <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=LastName}"/>
24: <TextBlock Grid.Row="2" Grid.Column="0" Text="Home Town:"/>
25: <TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Path=HomeTown}"/>
26: </Grid>
27: </Border>
28: </DataTemplate>
29: </Window.Resources>
컬렉션 바인딩
1: <StackPanel>
2: <TextBlock FontFamily="Verdana" FontSize="11" Margin="5,15,0,10" FontWeight="Bold">My Friends:</TextBlock>
3: <ListBox Width="200" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Source={StaticResource MyFriends}}" />
4: <TextBlock FontFamily="Verdana" FontSize="11" Margin="5,15,0,10" FontWeight="Bold">Information:</TextBlock>
5: <ContentControl Content="{Binding Source={StaticResource MyFriends}}" ContentTemplate="{StaticResource DetailTemplate}" />
6: </StackPanel>
댓글
댓글 쓰기