none
ListView的GroupStyle,也就是分组,如何对组进行增删改查? RRS feed

  • 问题

  • 我想对ListView的组进行操作。

    需求:

     增:可以新建一个组。内容可以没有。以后可以在ListView绑定的数据集合中添加.

     删:删除一个组,删除ListView绑定数据集合中与该组相关的数据对象。因为绑定的关系,被该数据对象绑定的组控件也会被删除

     改:更改组名,但不能和其他组重名

     查:点击组(非展开按钮),该组名被ViewModel类对象的某个属性获得,

    主要问题:

    实体类要怎么设计?


     这是我设计的实体类, groupName属性 就是组名,按照每个User对象的groupName 属性进行分组。但问题是不能新建一个内容可以没有的组,也不知道通过右键菜单删除怎么删除一个组。

    public class User
        {
            public string Name { get; set; }

            public int Age { get; set; }

            public string Mail { get; set; }

            public string groupName { get; set; }
        }



    2016年10月26日 11:42

答案

  • Hi 轮回的齿轮,

    跟你的描述,我建议你的实体类User需要实现INotifyPropertyChanged接口,并使ObservableCollection<T>作为集合,这个集合提供自动更新数据的接口,可以自动向控件发送更新消息。这样你删除,增加新的Items的时候,会自动更新IU。

    我做了一些小demo。你可以根据代码的示例,自行增加删除,更改项,增加新组,删除所有组以及其他操作(其实就是对集合进行操作)。

    你可以参考下面的代码:

    XAML:

    <Window.Resources>
            <CollectionViewSource
                x:Key='src' Source="{Binding   }">
                <CollectionViewSource.GroupDescriptions>
                    <PropertyGroupDescription PropertyName="groupName"/>
                </CollectionViewSource.GroupDescriptions>
            </CollectionViewSource>
        </Window.Resources>
        <Grid>
            <ListView Name="listViewTask" ItemsSource="{Binding  Source={StaticResource src}}"
                      BorderThickness="0" SelectionChanged="listViewTask_SelectionChanged" Margin="0,0,21,84"  >
                <ListView.GroupStyle>
                    <GroupStyle>
                        <GroupStyle.ContainerStyle>
                            <Style TargetType="{x:Type GroupItem}">
                                <Setter Property="Margin" Value="0,0,0,5"/>
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type GroupItem}">
    
                                            <Expander IsExpanded="True" BorderBrush="Red" BorderThickness="2">
                                                <Expander.Header>
                                                    <DockPanel>
                                                        <TextBlock FontWeight="Bold" Text="{Binding Path=Name}"
                                                                   Margin="5,0,0,0" Width="100"/>
                                                        <TextBlock FontWeight="Bold" Text="{Binding Path=ItemCount}"/>
                                                    </DockPanel>
                                                </Expander.Header>
                                                <Expander.Content>
                                                    <ItemsPresenter/>
                                                </Expander.Content>
                                            </Expander>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </GroupStyle.ContainerStyle>
                    </GroupStyle>
                </ListView.GroupStyle>
                <ListView.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="右键事件1" Click="MenuItem_Click"></MenuItem>
                        <MenuItem Header="右键事件2"  Click="MenuItem_Click_1"></MenuItem>
                    </ContextMenu>
                </ListView.ContextMenu>
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=Name }" Width="100"/>
                        <GridViewColumn Header="Age" DisplayMemberBinding="{Binding Path=Age}" Width="140"/>
                        <GridViewColumn Header="Mail" DisplayMemberBinding="{Binding Path=Mail}" Width="100"/>
                    </GridView>
                </ListView.View>
            </ListView>
            <Button x:Name="button" Content="Delete OneItem" HorizontalAlignment="Left" Margin="45,345,0,0" VerticalAlignment="Top" 
    
    Width="75" Click="button_Click"/>
            <Button x:Name="button1" Content="Add New Group" HorizontalAlignment="Left" Margin="179,345,0,0" VerticalAlignment="Top" 
    
    Width="75" Click="button1_Click"/>
            <Button x:Name="button2" Content="Delete oneGroup" HorizontalAlignment="Left" Margin="313,345,0,0" VerticalAlignment="Top" 
    
    Width="75" Click="button2_Click"/>
        </Grid>

    XAML.CS:

    /// <summary>
        /// ListViewCollection.xaml 的交互逻辑
        /// </summary>
        public partial class ListViewCollection : Window
        {
            public ObservableCollection<User> usersss = new ObservableCollection<User>();
            public ListViewCollection()
            {
                InitializeComponent();
                usersss.Add(new User() { Age = 12, Mail = "testmail12", Name = "test1-2", groupName = "001" });
                usersss.Add(new User() { Age = 13, Mail = "testmail13", Name = "test1-2", groupName = "001" });
                usersss.Add(new User() { Age = 13, Mail = "testmail14", Name = "test1-3", groupName = "002" });
                usersss.Add(new User() { Age = 14, Mail = "testmail15", Name = "test1-4", groupName = "002" });
                usersss.Add(new User() { Age = 15, Mail = "testmail16", Name = "test1-5", groupName = "003" });
                DataContext = usersss;
    
            }
    
            private void listViewTask_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
    
            }
    
            /// <summary>
            /// test
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button_Click(object sender, RoutedEventArgs e)
            {
                var UserModeltepm = (ObservableCollection<User>)DataContext;
                User selectuser = listViewTask.SelectedItem as User;
                if (selectuser != null)
                {
                    UserModeltepm.Remove(selectuser);
                }
            }
    
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                var UserModeltepm = (ObservableCollection<User>)DataContext;
               
                if (UserModeltepm != null)
                {
                    UserModeltepm.Add(new User() {  Age=90, Mail="HELLOW" , Name="ASSTEST" , groupName= "003" });
                }
            }
    
            private void button2_Click(object sender, RoutedEventArgs e)
            {
                var UserModeltepm = (ObservableCollection<User>)DataContext;
    
                if (UserModeltepm != null)
                {
                    UserModeltepm.Add(new User() { Age = 90, Mail = "HELLOW", Name = "ASSTEST", groupName = "004" });
                }
            }
    
            private void MenuItem_Click(object sender, RoutedEventArgs e)
            {
    
            }
    
            private void MenuItem_Click_1(object sender, RoutedEventArgs e)
            {
    
            }
        }
    
        public class User : INotifyPropertyChanged
        {
            private string name;
            public string Name
            {
                get
                {
                    return name;
                }
                set
                {
                    if (name != value)
                    {
                        name = value;
                        RaisePropertyChanged("Name");
                    }
                }
            }
    
            private int age;
            public int Age
            {
                get
                {
                    return age;
                }
                set
                {
                    if (age != value)
                    {
                        age = value;
                        RaisePropertyChanged("Age");
                    }
                }
            }
    
            private string mail;
            public string Mail
            {
                get
                {
                    return mail;
                }
                set
                {
                    if (mail != value)
                    {
                        mail = value;
                        RaisePropertyChanged("Mail");
                    }
                }
            }
            private string groupname;
            public string groupName
            {
                get
                {
                    return groupname;
                }
                set
                {
                    if (groupname != value)
                    {
                        groupname = value;
                        RaisePropertyChanged("groupName");
                    }
                }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            public void RaisePropertyChanged(string propertyName)
            {
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }

    Best Regards,

    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    2016年10月27日 14:04
    版主