本文介绍了为什么WP8 LongListSelector不正确地重复使用CheckBox的Checked状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WP8 LongListSelector与以下模板:

I have a WP8 LongListSelector with the following template:

    <DataTemplate x:Key="ItemTemplate">
        <Grid Margin="0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="110"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <controls:BlockImageControl 
                        Grid.Column="0"
                        Width="110"
                        Height="110"
                        Background="Transparent" />
            <TextBlock x:Name="Name" 
                            Grid.Column="1"
                            Text="{Binding ScreenName}" 
                            FontSize="{StaticResource PhoneFontSizeLarge}"
                            FontWeight="Bold"
                            VerticalAlignment="Center"/>
            <CheckBox x:Name="Unblock" Grid.Column="2" VerticalAlignment="Center"
                      Tap="BlocksList_Tap"
                      IsChecked="false"
                      />
        </Grid>
    </DataTemplate>

正如你所看到的,每个单元格项目的末尾都有一个复选框,选择多个项目。默认情况下IsChecked为false。

As you can see there is a checkbox at the end of each cell item, which enables the user to select multiple items. IsChecked is false by default.

问题是,LongListSelector似乎正在缓存我的复选框的Checked状态。如果我检查的第一个,项目,然后向下滚动一下,约30左右的项目后,我看到另一个项目,我没有选择。其余的绑定工作。它就好像它忽略模板中的IsChecked属性。我试着绑定IsChecked属性到一个属性,没有运气。

The problem is that the LongListSelector seems to be caching the Checked state of my checkbox. If I check the very first, item, then scroll down partway, after about 30 or so items, I see another item checked which I did not select. The rest of the bindings work. It is as if it is ignoring the "IsChecked" property in the template. I tried binding the IsChecked attribute to a property, no luck.

有人知道这是否是错误,如果没有,我该如何纠正这种行为?

Does anyone know if this is a bug, and if not, how I can correct this behavior?

谢谢!

>

推荐答案

不是一个错误,虽然它可能看起来像一个错误。你看到的是ui虚拟化的效果,基本上LongListSelector回收数据模板,而不是创建新的,以提高性能。回收的一个已知的副作用是,如果你的数据模板包含控件,保持自己的状态,例如CheckBox,那个状态将继承到新的项目。

Not a bug, although it might look like a bug at first. What you see is the effect of ui virtualization, basically LongListSelector recycles data templates instead of creating new ones to improve performance. One known side effect of recycling is that if your data template contains controls that maintain their own state, CheckBox for example, that state will carry over to the new item.

解决这个问题你需要从外部管理控制状态,即在视图模型中。在您的特定情况下,CheckBox的IsChecked属性必须绑定到视图模型的属性。并确保使用双向绑定。

To solve this issue you need to manage control state externally, i.e. in the view model. In your particular case IsChecked property of CheckBox must be bound to a property of view model. And make sure to use two way binding.

这篇关于为什么WP8 LongListSelector不正确地重复使用CheckBox的Checked状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 22:32