1

这是场景。我有一个列表视图,根据最终用户提供的标准,它可能有数千条记录。之后使用复选框,用户可以对记录进行分组。因此,我在 xaml 中为 listview 定义了组样式,当用户选中复选框时,我将 PropertyGroupDescription 添加到 ListCollectionView。

问题是数据虚拟化被禁用,因为为 listview 定义的 groupstyle 使得整个记录加载过程非常缓慢。是否可以不提前定义 groupstyle 但在用户选中复选框时定义 groupstyle ?

谢谢,

4

1 回答 1

1

这是我现在采取的方法,以便在用户不“对记录进行分组”时获得数据虚拟化的好处。如果需要,我将在后面的代码中插入组样式。发布代码,以便对其他人有用。

清除 GroupDescription 的值不会启用数据虚拟化。但是清除 GroupStyle 集合会启用数据虚拟化。

请让我知道如何在 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();
    }
于 2013-03-18T20:07:11.173 回答