带有复选框的Windows phone 8.1列表框重复选择

本文关键字:列表 选择 复选框 Windows phone | 更新日期: 2023-09-27 18:29:48

我创建了一个简单的项目,它显示了一个包含复选框的列表框。当用户选择多个项目的复选框时,下面(显示区域之外)的一些复选框也会被选中,而无需用户交互。这种"幻影"选择发生在大约第30个位置的项目上。

我试过了:

  • 从类中删除接口INotifyPropertyChanged
  • 将ObservableCollection更改为List
  • 将ListView更改为GridView

没有效果。

Code:
Class_Test_CheckBox.cs
using System;
using System.ComponentModel;
namespace App_Text_CheckBox
{
    public class Class_Test_CheckBox : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private Int64 _idPoint;
        private string _textContent;
        private bool _selected;
        private int _tag;
        public Int64 IdPoint
        {
            get { return _idPoint; }
            set
            {
                if (_idPoint != value)
                {
                    _idPoint = value;
                    RaisePropertyChanged("IdPoint");
                }
            }
        }
        public string TextContent
        {
            get { return _textContent; }
            set
            {
                if (_textContent != value)
                {
                    _textContent = value;
                    RaisePropertyChanged("TextContent");
                }
            }
        }
        public bool Selected
        {
            get { return _selected; }
            set
            {
                if (_selected != value)
                {
                    _selected = value;
                    RaisePropertyChanged("Selected");
                }
            }
        }
        public int Tag
        {
            get { return _tag; }
            set
            {
                if (_tag != value)
                {
                    _tag = value;
                    RaisePropertyChanged("Tag");
                }
            }
        }
        protected void RaisePropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
    }
}
Page_Test_CheckBox.xaml:
<Page
    x:Class="App_Text_CheckBox.Page_Test_CheckBox"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App_Text_CheckBox"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Page.Resources>
        <!--Collection of items displayed by this page-->
        <CollectionViewSource x:Name="itemsTestCheckBox"/>
    </Page.Resources>
    <Grid x:Name="LayoutRoot">
        <Grid.ChildrenTransitions>
            <TransitionCollection>
                <EntranceThemeTransition/>
            </TransitionCollection>
        </Grid.ChildrenTransitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <!-- Title Panel -->
        <StackPanel Grid.Row="0" Margin="19,0,0,0">
            <TextBlock Text="TEST APPLICATION" Style="{ThemeResource TitleTextBlockStyle}" Margin="0,12,0,0"/>
            <TextBlock Text="Test CheckBox" Margin="0,-6.5,0,26.5" Style="{ThemeResource HeaderTextBlockStyle}" CharacterSpacing="{ThemeResource PivotHeaderItemCharacterSpacing}"/>
        </StackPanel>
        <!--TODO: Content should be placed within the following grid-->
        <Grid Grid.Row="1" x:Name="ContentRoot" Margin="19,9.5,19,0">
            <ListView
                ItemsSource="{Binding Source={StaticResource itemsTestCheckBox}}"
                ScrollViewer.HorizontalScrollMode="Disabled"
                ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                ScrollViewer.VerticalScrollMode="Auto"
                ScrollViewer.VerticalScrollBarVisibility="Auto" >
                <ListView.ItemTemplate >
                    <DataTemplate >
                        <Grid Margin="1">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="40"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Grid Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Left">
                                <CheckBox x:Name="cb_Test_CheckBox" Tag="{Binding Tag}" IsChecked="{Binding Selected}" Checked="cb_Test_CheckBox_Checked" Unchecked="cb_Test_CheckBox_Unchecked"  />
                            </Grid>
                            <TextBlock Grid.Column="1" Tag="{Binding Tag}" Text="{Binding TextContent}" TextWrapping="NoWrap" FontSize="16" VerticalAlignment="Center"/>
                        </Grid>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </Grid>
    </Grid>
</Page>
Page_Test_CheckBox.xaml.cs:
using App_Text_CheckBox.Common;
using System.Collections.ObjectModel;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
// The Basic Page item template is documented at http://go.microsoft.com/fwlink/?LinkID=390556
namespace App_Text_CheckBox
{
     public sealed partial class Page_Test_CheckBox : Page
    {
        private NavigationHelper navigationHelper;
        private ObservableDictionary defaultViewModel = new ObservableDictionary();
        // Test ObservableCollection
        public ObservableCollection<Class_Test_CheckBox> ObservableCollection_Test_CheckBox = new ObservableCollection<Class_Test_CheckBox>();
        public Page_Test_CheckBox()
        {
            this.InitializeComponent();
            this.navigationHelper = new NavigationHelper(this);
            this.navigationHelper.LoadState += this.NavigationHelper_LoadState;
            this.navigationHelper.SaveState += this.NavigationHelper_SaveState;
        }
        public NavigationHelper NavigationHelper
        {
            get { return this.navigationHelper; }
        }
        public ObservableDictionary DefaultViewModel
        {
            get { return this.defaultViewModel; }
        }
        private void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
        {
        }
        private void NavigationHelper_SaveState(object sender, SaveStateEventArgs e)
        {
        }
        #region NavigationHelper registration
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            this.navigationHelper.OnNavigatedTo(e);
            // Create 55 items
            for (int ii = 0; ii < 55; ii++)
            {
                Class_Test_CheckBox newItem = new Class_Test_CheckBox();
                newItem.IdPoint = (long)ii;
                newItem.Selected = false;       //for Binding CheckBox
                newItem.Tag = ii;
                newItem.TextContent = (ii + 1).ToString() + ". row - Lorem Ipsum Dolor ...";
                ObservableCollection_Test_CheckBox.Add(newItem);
            }
            //items -> source
            itemsTestCheckBox.Source = ObservableCollection_Test_CheckBox;
        }
        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            this.navigationHelper.OnNavigatedFrom(e);
        }
        #endregion
        private void cb_Test_CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            int index = (int)((sender as CheckBox).Tag);
            ObservableCollection_Test_CheckBox[index].Selected = true;
            ObservableCollection_Test_CheckBox[index].TextContent = (index + 1).ToString() + ". row - LOREM IPSUM DOLOR " + index.ToString() + " " + ObservableCollection_Test_CheckBox[index].Selected.ToString();
        }
        private void cb_Test_CheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            int index = (int)((sender as CheckBox).Tag);
            ObservableCollection_Test_CheckBox[index].Selected = false;
            ObservableCollection_Test_CheckBox[index].TextContent = (index + 1).ToString() + ". row - lorem ipsum dolor " + index.ToString() + " " + ObservableCollection_Test_CheckBox[index].Selected.ToString();
        }
    }
}

是否有任何错误,或者如何避免重复选择?

带有复选框的Windows phone 8.1列表框重复选择

我也遇到了这个问题。此问题似乎是启用虚拟化的结果。有一个变通方法,但我仍然认为这是一个错误。

这是关于这个问题的链接: