■ GroupStyle 엘리먼트 : HeaderTemplate 속성을 사용해 ListBox 객체에서 그룹 스타일을 설정하는 방법을 보여준다.
▶ 예제 코드 (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 |
... <ListBox Name="listBox"> <ListBox.ItemTemplate> ... </ListBox.ItemTemplate> <ListBox.GroupStyle> <GroupStyle> <GroupStyle.HeaderTemplate> <DataTemplate> <TextBlock Margin="6" Foreground="White" Background="DarkGray" FontWeight="Bold" FontSize="12pt" Text="{Binding Path=Name}" /> </DataTemplate> </GroupStyle.HeaderTemplate> </GroupStyle> </ListBox.GroupStyle> </ListBox> ... |
▶ 예제 코드 (C#)
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 |
using System; using System.Collections.ObjectModel; using System.ComponentModel; using System.Globalization; using System.Windows.Data; ... /// <summary> /// 기간 그룹 설명자 /// </summary> public class PeriodGroupDescription : GroupDescription { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 항목에서 그룹 명칭 구하기 - GroupNameFromItem(item, level, cultureInfo) /// <summary> /// 항목에서 그룹 명칭 구하기 /// </summary> /// <param name="item">항목</param> /// <param name="level">레벨</param> /// <param name="cultureInfo">CultureInfo 객체</param> /// <returns>그룹 명칭</returns> public override object GroupNameFromItem(object item, int level, CultureInfo cultureInfo) { Person person = item as Person; if(person.BirthDate == null) { return "Unknown"; } int year = ((DateTime)person.BirthDate).Year; if(year < 1575) return "Pre-Baroque"; if(year < 1725) return "Baroque"; if(year < 1795) return "Classical"; if(year < 1870) return "Romantic"; if(year < 1910) return "20th Century"; return "Post-War"; } #endregion } ... ObservableCollection<Person> observableCollection = new ObservableCollection<Person>(); ... ListCollectionView listCollectionView = new ListCollectionView(observableCollection); listCollectionView.SortDescriptions.Add(new SortDescription("BirthDate", ListSortDirection.Ascending)); listCollectionView.GroupDescriptions.Add(new PeriodGroupDescription()); ... this.listBox.ItemsSource = listCollectionView; if(this.listBox.Items.Count > 0) { this.listBox.SelectedIndex = 0; } |