wpf2009. 9. 21. 16:34
bservable Collection을 이용한 바인딩 샘플

파일>새로만들기>프로젝트
 순서로 누른다 .

 

 

 

 

 

 

 

 

 

[확인]을 눌러서 프로젝트를 생성한다.

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>

끝으로 실행해 본다. ... 아래와 같이 뜨면 잘한겨..


이 글과 관련있는 글을 자동검색한 결과입니다 [?]




















Posted by 동동(이재동)