2013-03-18 15 views
1

這是場景。我有一個listview可能有成千上萬的記錄基於最終用戶提供的標準。之後使用複選框,用戶可以對記錄進行分組。所以,我在xaml中爲listview定義了組風格,當用戶選中複選框時,我將PropertyGroupDescription添加到ListCollectionView中。可以在運行時爲ListView定義GroupStyle嗎?

問題是數據虛擬化被禁用,因爲爲listview定義了groupstyle,這使得整個記錄加載過程非常緩慢。是否有可能不提前定義groupstyle,而是在用戶選中複選框時定義groupstyle?

感謝,

回答

1

這是我採取了現在得到的數據虛擬化的好處,而用戶沒有辦法「分組的記錄。」如果需要,我在後面的代碼中插入Group Style。發佈代碼,以便對其他人有用。

清除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(); 
    } 
相關問題