■ Binding 태그 확장의 Source 속성을 사용해 컬렉션을 바인딩하고 선택 정보를 표시하는 방법을 보여준다.
▶ Person.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
using System.ComponentModel; namespace TestProject { /// <summary> /// 사람 /// </summary> public class Person : INotifyPropertyChanged { //////////////////////////////////////////////////////////////////////////////////////////////////// Event ////////////////////////////////////////////////////////////////////////////////////////// Public #region 속성 변경시 - PropertyChanged /// <summary> /// 속성 변경시 /// </summary> public event PropertyChangedEventHandler PropertyChanged; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 이름 /// </summary> private string firstName; /// <summary> /// 성 /// </summary> private string lastName; /// <summary> /// 고향 /// </summary> private string homeTown; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 이름 - FirstName /// <summary> /// 이름 /// </summary> public string FirstName { get { return this.firstName; } set { this.firstName = value; FirePropertyChangedEvent("FirstName"); } } #endregion #region 성 - LastName /// <summary> /// 성 /// </summary> public string LastName { get { return this.lastName; } set { this.lastName = value; FirePropertyChangedEvent("LastName"); } } #endregion #region 고향 - HomeTown /// <summary> /// 고향 /// </summary> public string HomeTown { get { return this.homeTown; } set { this.homeTown = value; FirePropertyChangedEvent("HomeTown"); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - Person() /// <summary> /// 생성자 /// </summary> public Person() { } #endregion #region 생성자 - Person(firstName, lastName, homeTown) /// <summary> /// 생성자 /// </summary> /// <param name="firstName">이름</param> /// <param name="lastName">성</param> /// <param name="homeTown">고향</param> public Person(string firstName, string lastName, string homeTown) { this.firstName = firstName; this.lastName = lastName; this.homeTown = homeTown; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 문자열 구하기 - ToString() /// <summary> /// 문자열 구하기 /// </summary> /// <returns></returns> public override string ToString() => this.firstName; #endregion ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 속성 변경시 이벤트 발생시키기 - FirePropertyChangedEvent(propertyName) /// <summary> /// 속성 변경시 이벤트 발생시키기 /// </summary> /// <param name="propertyName">속성명</param> protected void FirePropertyChangedEvent(string propertyName) { PropertyChangedEventHandler handler = PropertyChanged; handler?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } #endregion } } |
▶ PersonCollection.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
using System.Collections.ObjectModel; namespace TestProject { /// <summary> /// 사람 컬렉션 /// </summary> public class PersonCollection : ObservableCollection<Person> { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - PersonCollection() /// <summary> /// 생성자 /// </summary> public PersonCollection() { Add(new Person("Michael" , "Alexander", "Bellevue")); Add(new Person("Jeff" , "Hay" , "Redmond" )); Add(new Person("Christina", "Lee" , "Kirkland")); Add(new Person("Samantha" , "Smith" , "Seattle" )); } #endregion } } |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:TestProject" Width="800" Height="600" Title="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <Window.Resources> <local:PersonCollection x:Key="PersonCollectionKey" /> <Style TargetType="ListBoxItem"> <Setter Property="Padding" Value="10" /> </Style> <DataTemplate x:Key="DetailTemplateKey"> <Border Width="300" Height="100" BorderThickness="5" BorderBrush="RoyalBlue" Padding="10"> <Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <TextBlock Grid.Row="0" Grid.Column="0" FontWeight="Bold" Text="이름" /> <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=FirstName}" /> <TextBlock Grid.Row="1" Grid.Column="0" FontWeight="Bold" Text="성" /> <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=LastName}" /> <TextBlock Grid.Row="2" Grid.Column="0" FontWeight="Bold" Text="고향" /> <TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Path=HomeTown}" /> </Grid> </Border> </DataTemplate> </Window.Resources> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <TextBlock HorizontalAlignment="Center" Margin="10" FontWeight="Bold"> 친구 목록 </TextBlock> <ListBox Width="200" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Source={StaticResource PersonCollectionKey}}" /> <TextBlock HorizontalAlignment="Center" Margin="10" FontWeight="Bold"> 정보 </TextBlock> <ContentControl ContentTemplate="{StaticResource DetailTemplateKey}" Content="{Binding Source={StaticResource PersonCollectionKey}}" /> </StackPanel> </Window> |