C#/WPF
WPF tutorial - ListView grouping
광그로
2017. 7. 4. 14:35
http://www.wpf-tutorial.com/listview-control/listview-grouping/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <Grid Margin="10"> <ListView Name="lvUsers"> <ListView.View> <GridView> <GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" /> <GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Age}" /> </GridView> </ListView.View> <ListView.GroupStyle> <GroupStyle> <GroupStyle.HeaderTemplate> <DataTemplate> <TextBlock FontWeight="Bold" FontSize="14" Text="{Binding Name}"/> </DataTemplate> </GroupStyle.HeaderTemplate> </GroupStyle> </ListView.GroupStyle> </ListView> </Grid> | 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 | public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); List<User> items = new List<User>(); items.Add(new User() { Name = "John Doe", Age = 42, Sex = SexType.Male }); items.Add(new User() { Name = "Jane Doe", Age = 39, Sex = SexType.Female }); items.Add(new User() { Name = "Sammy Doe", Age = 13, Sex = SexType.Male }); lvUsers.ItemsSource = items; CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lvUsers.ItemsSource); PropertyGroupDescription groupDescription = new PropertyGroupDescription("Sex"); view.GroupDescriptions.Add(groupDescription); } } public enum SexType { Male, Female }; public class User { public string Name { get; set; } public int Age { get; set; } public string Mail { get; set; } public SexType Sex { get; set; } } } | cs |
1 2 3 4 | <ListView.GroupStyle> <GroupStyle> <GroupStyle.HeaderTemplate> <DataTemplate> | cs |
<TextBlock Text="{Binding Name}"/>TextBlock의 Text는 Name에 바인딩이 되어있는 상태이지만, data 객체는 Name 속성이 아닙니다.
CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lvUsers.ItemsSource);
ItemsSources를 할당한 후에는 ListView가 생성한 CollectionView를 이용합니다.
Customizing the group header
어.. 뭐지.. 좀 복잡하다 계속 공부해봐야겠습니다.
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 | <Grid Margin="10"> <ListView Name="lvUsers"> <ListView.View> <GridView> <GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" /> <GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Age}" /> </GridView> </ListView.View> <ListView.GroupStyle> <GroupStyle> <GroupStyle.ContainerStyle> <Style TargetType="{x:Type GroupItem}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate> <Expander IsExpanded="True"> <Expander.Header> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="Gray" FontSize="22" VerticalAlignment="Bottom" /> <TextBlock Text="{Binding ItemCount}" FontSize="22" Foreground="Green" FontWeight="Bold" FontStyle="Italic" Margin="10,0,0,0" VerticalAlignment="Bottom" /> <TextBlock Text=" item(s)" FontSize="22" Foreground="Silver" FontStyle="Italic" VerticalAlignment="Bottom" /> </StackPanel> </Expander.Header> <ItemsPresenter /> </Expander> </ControlTemplate> </Setter.Value> </Setter> </Style> </GroupStyle.ContainerStyle> </GroupStyle> </ListView.GroupStyle> </ListView> </Grid> | cs |