7

I have some troubles with custom ListView (CheckBoxes inside lost their states when scrolling ListView). Can I disable ListView virtualization without visual tree enumerating?

<ListView>
    <ListView.Resources>
        <Style TargetType="GridViewColumnHeader">
            <Setter Property="Visibility" Value="Collapsed" />
        </Style>
    </ListView.Resources>
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Address">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox Content="{Binding Address}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Header="Name">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Name}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>
Oomph Sonar
  • 783
  • 2
  • 8
  • 11
  • 2
    Don't disable virtualization. Simply bind the `IsChecked` property of the checkbox to some relevant boolean value. – Federico Berasategui Apr 29 '14 at 17:55
  • possible duplicate of [ListBox is virtual by default?](http://stackoverflow.com/questions/13326506/listbox-is-virtual-by-default) – Rohit Vats Apr 29 '14 at 18:01
  • It's good idea, about a binding, but when I'm changing private properties of ListView in Snoop (VirtualizingPanel.IsVirtualising and VirtualizingPanel.IsContainerVirtualizable) all works right. – Oomph Sonar Apr 29 '14 at 18:26
  • See my answer. Try turning off recycling. – paparazzo Apr 29 '14 at 18:27
  • HariSeldon turning off virtualization is BAD. You will have serious performance issues if your `ListView` shows many records. and you're simply doing a HACK instead of implementing a proper solution. – Federico Berasategui Apr 29 '14 at 18:27
  • 2
    I have only about 50 records in ListView. Perfomance not important. – Oomph Sonar Apr 29 '14 at 18:33

2 Answers2

17

As stated by H.B. in their answer here:

Set VirtualizingStackPanel.IsVirtualizing to false on the ListView or set a normal StackPanel as the ListView.ItemsPanel.

<ListView VirtualizingStackPanel.IsVirtualizing="False"/>

or

<ListView>
 <ListView.ItemsPanel>
     <ItemsPanelTemplate>
       <StackPanel></StackPanel>
      </ItemsPanelTemplate>
  </ListView.ItemsPanel>
 </ListView>
Community
  • 1
  • 1
123 456 789 0
  • 10,565
  • 4
  • 43
  • 72
  • 1
    It's important to mention that this is a HACK and should be avoided. The OP's problem is the lack of DataBinding for the `CheckBox.IsChecked` property, disabling virtualization is no solution. – Federico Berasategui Apr 29 '14 at 18:02
  • 1
    @HighCore Still a good thing to know that you can turn off virtualization. You don't want virtualized all the time. – 123 456 789 0 Apr 29 '14 at 18:08
  • `You don't want virtualized all the time.` - sorry, not true. I can't think of a valid justification to turn of virtualization in WPF. – Federico Berasategui Apr 29 '14 at 18:13
  • @HighCore An important consideration when you virtualize item containers is whether you have additional state information associated with an item container that belongs with the item. In this case, you must save the additional state. For example, you might have an item contained in an Expander control and the IsExpanded state is bound to the item's container, and not to the item itself. When the container is reused for a new item, the current value of IsExpanded is used for the new item. In addition, the old item loses the correct IsExpanded value. – 123 456 789 0 Apr 29 '14 at 18:39
  • 1
    `additional state information associated with an item container` - you could always use attached properties or something. Disabling virtualization is a HACK. sorry. – Federico Berasategui Apr 29 '14 at 18:44
  • 1
    @HighCore Not true with `TreeView` with 1-n children and don't want to use `Recycling/Standard` and don't want to clutter my things with attach properties, sorry. Not especially if it is not a requirement. `KISS`. – 123 456 789 0 Apr 29 '14 at 18:47
  • We're not talking about `TreeView`. I guess your treeview implementation works really well with a million items. goodbye. – Federico Berasategui Apr 29 '14 at 18:48
  • @HighCore Also, if you virtualized what if you want to just load 100 items and then you want to scroll to the item right away? You'll have to dig into the inner content from the virtualization panel if virtualization is on and derealize the item just to scroll into it. Disabling virtualization will load just make me scroll me to that item right away. – 123 456 789 0 Apr 29 '14 at 18:48
  • 1
    @HighCore Stop saying `disabling virtualization is no solution` then. Because that is not always the case. :D – 123 456 789 0 Apr 29 '14 at 18:49
  • 3
    As a reminder, when you copy the words of others, you must provide proper attribution, like I've done in the above. – Brad Larson Apr 29 '14 at 19:07
  • While I was searching for how to enable virtualization, because it did not work at mine, I see you can disable it with using `StackPanel` inside `ItemsPanelTemplate`. Is `WrapPanel` causing the same issue? And my listview only has `VirtualizingPanel.IsVirtualizing`, `VirtualizingStackPanel`is present but there is no property `IsVirtualizing`, only one event handler. – CularBytes May 08 '16 at 22:17
-1

In addition to binding the IsChecked property

You also may need to turn off recycling

VirtualizingStackPanel.VirtualizationMode="Standard" 

VirtualizationMode Enumeration

paparazzo
  • 44,497
  • 23
  • 105
  • 176
  • But something strange for me: when I'm looking inside ListView in Snoop it showing VirtualizingPanel instead VirtualizingStackPanel. – Oomph Sonar Apr 29 '14 at 18:36
  • How is that an answer? It should be a comment to above answer at most. Unless you have something in mind, but I am not able to read it. – Sinatr Dec 07 '17 at 09:16