파일>새로만들기>프로젝트 순서로 누른다 .
[확인]을 눌러서 프로젝트를 생성한다.
Person 클래스 파일을 추가한다. System.ComponetModel과 System.Collections.ObjectModel을 추가하고 다음의 코드를 완성한다.
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
namespace ObservableCollection
{
public class Person : INotifyPropertyChanged
{
#region INotifyPropertyChanged 멤버
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string info)
{
if(PropertyChanged!=null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
#endregion
public Person()
{
}
public Person(string first, string last, string town)
{
this.firstname = first;
this.lastname = last;
this.hometown = town;
}
public override string ToString()
{
return firstname.ToString();
}
private string firstname;
public string FirstName
{
get { return firstname; }
set {
firstname = value;
OnPropertyChanged("FirstName");
}
}
private string lastname;
public string LastName
{
get { return lastname; }
set {
lastname = value;
OnPropertyChanged("LastName");
}
}
private string hometown;
public string HomeTown
{
get { return hometown; }
set {
hometown = value;
OnPropertyChanged("HomeTown");
}
}
}
public class People : ObservableCollection<Person>
{
public People() : base()
{
Add(new Person("Michael", "Alexander", "Bellevue"));
Add(new Person("Jeff", "Hay", "Redmond"));
Add(new Person("Christina", "Lee", "Kirkland"));
Add(new Person("Samantha", "Smith", "Seattle"));
}
};
}
Window1.xaml 파일을 아래와 같이 수정한다.
<Window x:Class="ObservableCollection.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Binding to a Collection"
xmlns:local="clr-namespace:ObservableColleciton"
SizeToContent="WidthAndHeight"
>
</Window>
Window1.xaml에 리소스를 추가한다.
<Window.Resources>
<ObjectDataProvider x:Key="MyFriends" ObjectType="{x:Type local:People}"/>
<Style TargetType="ListBoxItem">
<Setter Property="FontFamily" Value="Verdana"/>
<Setter Property="FontSize" Value="11"/>
<Setter Property="Padding" Value="10"/>
</Style>
<DataTemplate x:Key="DetailTemplate">
<Border Width="300" Height="100" Margin="20"
BorderBrush="LightSeaGreen" BorderThickness="3" Padding="8"
CornerRadius="5">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="First Name:"/>
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=FirstName}"/>
<TextBlock Grid.Row="1" Grid.Column="0" Text="Last Name:"/>
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=LastName}"/>
<TextBlock Grid.Row="2" Grid.Column="0" Text="Home Town:"/>
<TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Path=HomeTown}"/>
</Grid>
</Border>
</DataTemplate>
</Window.Resources>
끝으로 컨트롤을 작성한다.
<StackPanel>
<TextBlock FontFamily="Verdana" FontSize="11"
Margin="5,15,0,10" FontWeight="Bold">My Friends</TextBlock>
<ListBox Width="200" IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Source={StaticResource MyFriends}}"/>
<TextBlock FontFamily="Verdana" FontSize="11"
Margin="5,15,0,5" FontWeight="Bold">Information</TextBlock>
<ContentControl Content="{Binding Source={StaticResource MyFriends}}"
ContentTemplate="{StaticResource DetailTemplate}"/>
</StackPanel>
끝으로 실행해 본다. ... 아래와 같이 뜨면 잘한겨..
'wpf' 카테고리의 다른 글
[wpf] INotifyPropertyChanged과 Observable Collection을 이용한 바인딩 샘플파일 (0) | 2009.09.21 |
---|---|
[wpf] delegate 쓸때 파라미터 쓰는 메소드 만들기 (0) | 2009.09.21 |
[wpf] xml 만들어서 file로 쓰기 (1) | 2009.09.16 |
[wpf] MenuItem Binding걸어서 Click Event 받아오기(어느메뉴를 클릭했는지..) (0) | 2009.09.10 |
[wpf] Regex를 이용해서 string 문자를 치환하자... (0) | 2009.09.04 |
Posted by 동동(이재동)