11. 가정
class Chart
+ Album FirstAlbum
class Album
+ List<Album> Albums
+ string CoverArt
+ string Name
+ Artist Artist class Artist
+ string ProfilerImage
+ string Name
19. ItemsControl 가족
• ListView Control
• GridView
• FlipView
• ListBox ItemsControl .ItemsSource 프로퍼티가 여기 정의
• ComboBox
Selector
ListViewBase FlipView ListBox ComboBox
ListView GridView
20. ItemsControl에서 DataContext 분배
CS에서
var artists = new List<Artist>()
{ 싸이
new Artist() { Name = “싸이”, CoverArt=“…”},
new Artist() { Name = “아이
유”, CoverArt=“…”}, 아이유
new Artist() { Name = “싸이”, CoverArt=“…”},
new Artist() { Name = “아이
유”, CoverArt=“…”},
} 싸이
this.Artists = artist;
….
XAML에서
아이유
<ListView ItemsSource=“{Binding Artists}” />
21. ItemTemplate과 DataContext
new Artist()
{ 싸이
Name = “싸이”,
CoverArt=“…”,
} <ListView.ItemTemplate>
<DataTemplate>
ItemsSource의 인스턴스 하나가 <Grid>
ListViewItem 하나의 DataContext가 <StackPanel>
된다. <Image Source=“{Binding CoverArt}”
/>
<TextBlock Text=“{Binding Name}” />
</StackPanel>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
22. In the hood
ItemsControl의 virtual PrepareContainerForItemOverride(…) 에서
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
var contentControl = element as ContentControl;
contentControl.ContentTemplate = this.ItemTemplate;
contentControl.DataContext = item;
}
24. 약속
컨트롤은 INotifyPropertyChanged.PropertyChanged를 구독합니다.
컨트롤은 INotifyCollectionChanged.CollectionChanged를 구독합니다.
Common/BindableBase.cs 에서
public abstract class BindableBase : INotifyPropertyChanged
System.Collections.ObjectModel
public class ObservableCollection<T> : Collection<T>, INotifyCollectionChanged
25. 이미 구현되어 있는 것
DataModel/SampleDataSource.cs에서
public abstract class SampleDataCommon : App4.Common.BindableBase
프로퍼티 예 : Title
private string _title = string.Empty;
public string Title
{
get { return this._title; }
set { this.SetProperty(ref this._title, value); }
}
In the Hood
protected bool SetProperty<T>(ref T storage, T value,
[CallerMemberName] String propertyName = null)
{
if (object.Equals(storage, value)) return false;
storage = value;
this.OnPropertyChanged(propertyName);
return true;
}
28. 어떤 필요, 어떤 니즈?
public List<string> Artists { get; set; }
…
Artists = new List<string>()
{
“싸이”,
“아이유”,
};
너랑 나랑 강남스타일
싸이, 아이유
29. 샘플 ArtistConverter
namespace MyApp
{
public class ArtistConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
// null 체크, value가 Ienumerable 타입이 아닐 때 예외처리 (생략)
var list = value as IEnumerable;
StringBuilder sb = new StringBuilder();
foreach (var item in list)
{
if (sb.Length > 0)
sb.Append(“, “);
sb.Append((string)item);
}
return sb.ToString();
}
}
}
30. 사용법
인스턴스 생성 (어딘가에) -> 바인딩 식에서 잘 사용
In MyView.xaml (or App.xaml)
<Page>
<Page.Resources>
<conv:ArtistConverter x:Key=“ArtistConverter”/>
</Page.Resources>
<Grid x:Name=“LayoutRoot”>
…
<TextBlock Text=“{Binding Artists,
너랑 나랑 강남스타일
Converter={StaticResource ArtistConverter}”/> 싸이, 아이유
</Grid>
</Page>