简体   繁体   English

可以在运行时为listview定义GroupStyle吗?

[英]Can the GroupStyle be defined at runtime for listview?

Here is the scenario. 这是场景。 I have a listview which might have thousands of records based on the criteria provided by the end user. 我有一个listview,根据最终用户提供的条件,它可能有成千上万条记录。 After that using a checkbox, user can group records. 之后,使用复选框,用户可以对记录进行分组。 So, I have group style defined in xaml for the listview and when user checks the checkbox, I add PropertyGroupDescription to ListCollectionView. 因此,我在xaml中为listview定义了组样式,并且当用户选中复选框时,我将PropertyGroupDescription添加到ListCollectionView。

The problem is data virtualization is disabled because of the groupstyle defined for listview which makes the entire record loading process very slow. 问题是由于为listview定义了groupstyle,因此禁用了数据虚拟化,这使得整个记录加载过程非常缓慢。 Is it possible to not to define groupstyle in advance but define the groupstyle when the checkbox is checked by the user? 当用户选中复选框时,是否可以不预先定义groupstyle而是定义groupstyle?

Thanks, 谢谢,

This is the approach I have taken right now to get the benefit of data virtualization while user is not "Grouping the records." 这是我现在采取的方法,可以在用户未对记录进行分组的同时获得数据虚拟化的好处。 I am inserting the Group Style in code behind if required. 如果需要,我将在代码后面插入组样式。 Posting the code so it might be useful for someone else. 发布代码,这样对其他人可能有用。

Clearing the values of GroupDescription does not enable the data virtualization. 清除GroupDescription的值不会启用数据虚拟化。 But clearing the GroupStyle collection enableds the data virtualization. 但是清除GroupStyle集合将启用数据虚拟化。

Please let me know how to handle it in XAML. 请让我知道如何在XAML中处理它。

private GroupStyle retrieveGroupStyle()
    {
        #region  Control template Code(to show content)

        ControlTemplate template = new ControlTemplate(typeof(GroupItem));

        //Create border object
        FrameworkElementFactory border = new FrameworkElementFactory(typeof(Border));


        border.SetValue(Border.CornerRadiusProperty, new CornerRadius(3));
        border.SetValue(Border.BorderThicknessProperty, new Thickness(2));
        border.SetValue(Border.BorderBrushProperty, new SolidColorBrush(Colors.Silver));
        border.SetValue(Border.PaddingProperty, new Thickness(2));

        //create dockpanel to put inside border.
        FrameworkElementFactory dockPanel = new FrameworkElementFactory(typeof(DockPanel));
        dockPanel.SetValue(DockPanel.LastChildFillProperty, true);

        //stack panel to show group header
        FrameworkElementFactory stackPanel = new FrameworkElementFactory(typeof(StackPanel));
        stackPanel.SetValue(StackPanel.OrientationProperty, Orientation.Vertical);
        stackPanel.SetValue(DockPanel.DockProperty, Dock.Top);

        //Create textBlock to show group header
        FrameworkElementFactory textBlock = new FrameworkElementFactory(typeof(TextBlock));

        textBlock.SetValue(TextBlock.PaddingProperty, new Thickness(2));
        textBlock.SetValue(TextBlock.FontWeightProperty, FontWeights.Bold);
        textBlock.SetValue(TextBlock.TextProperty, new Binding() { Path = new PropertyPath("Name") });
        stackPanel.AppendChild(textBlock);
        template.VisualTree = border;
        //define items presenter
        FrameworkElementFactory itemsPresenter = new FrameworkElementFactory(typeof(ItemsPresenter));
        itemsPresenter.SetValue(ItemsPresenter.MarginProperty, new Thickness(2));

        border.AppendChild(dockPanel);

        dockPanel.AppendChild(stackPanel);
        dockPanel.AppendChild(itemsPresenter);

        template.VisualTree = border;

        #endregion


        #region Set container style for Group

        Style style = new Style(typeof(GroupItem));

        Setter setter = new Setter();
        setter.Property = GroupItem.TemplateProperty;
        setter.Value = template;

        style.Setters.Add(setter);

        GroupStyle groupStyle = new GroupStyle();

        groupStyle.ContainerStyle = style;

        #endregion

        return groupStyle;
    }

    /// <summary>
    /// Adds the group style (once the group styles are defined virtualization is disabled.)
    /// </summary>
    private void addGroupStyle()
    {
        this.listView1.GroupStyle.Add(this.retrieveGroupStyle());
        this.listView2.GroupStyle.Add(this.retrieveGroupStyle());
    }
    /// <summary>
    /// Removes the group style. (once the group style is removed virtualization is enabled.)
    /// </summary>
    private void removeGroupStyle()
    {
        this.listView1.GroupStyle.Clear();
        this.listView2.GroupStyle.Clear();
    }
    private void CheckBox_Checked_1(object sender, RoutedEventArgs e)
    {
        this.addGroupStyle();
    }

    private void CheckBox_Unchecked_1(object sender, RoutedEventArgs e)
    {
        this.removeGroupStyle();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM