none
windows的分组用wpf实现的话,有什么思路么? RRS feed

  • 问题

  • 类似上面这种,这是 如何实现的?每个treeViewitem下面都弄一个listview?ListView本身有没有类似功能?或者降低下问题的复杂性,不设计每组的展开和收缩这两个功能,在每组上面弄一段文字,listview中怎么做到?(分组的结果不必存入数据库),


    2016年9月16日 2:51

答案

  • 您好 轮回的齿轮,

    >>"ListView本身有没有类似功能?"

    WPF中ListView的确也提供了这个功能,我们可以通过ListView的GroupStyle属性来设置分组的样式。同时ListView的数据源还需要使用CollectionView来设置分组方式,以下示例供您参考。

    <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" />
                                            </StackPanel>
                                        </Expander.Header>
                                        <ItemsPresenter />
                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </ListView.GroupStyle>
    </ListView>
    public partial class TestGroup : Window
    {
        public TestGroup()
        {
            InitializeComponent();
    
            List<User> items = new List<User>();
            items.Add(new User() { Name = "John", Age = 42, Gender = GenderType.Male });
            items.Add(new User() { Name = "Jane", Age = 39, Gender = GenderType.Female });
            items.Add(new User() { Name = "Sammy", Age = 13, Gender = GenderType.Male });
            lvUsers.ItemsSource = items;
    
            CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lvUsers.ItemsSource);
            PropertyGroupDescription groupDescription = new PropertyGroupDescription("Gender");
            view.GroupDescriptions.Add(groupDescription);
        }
    
        public enum GenderType { Male, Female };
    
        public class User
        {
            public string Name { get; set; }
    
            public int Age { get; set; }
    
            public string Mail { get; set; }
    
            public GenderType Gender { get; set; }
        }
    }
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2016年9月16日 7:18