WPFDevelopersOrg / WPFDevelopers

WPF Developers Daily Share 🎉Ongoing update.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

分页组件跳转到最后一页有闪烁并会回退到第一页

fzxbl237 opened this issue · comments

image
当 count=5;countPerPage=4的时候,点击第二个listboxitem会有闪烁并回退到第一页的问题

麻烦提供下demo,或者把代码提供下。

麻烦提供下demo,或者把代码提供下。

MainWindow.xaml

<Window
    x:Class="WpfApp5.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:WpfApp5"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:wd="https://github.com/WPFDevelopersOrg/WPFDevelopers"
    Title="MainWindow"
    Width="800"
    Height="450"
    mc:Ignorable="d">
    <Grid>
        <StackPanel>
            <ListBox Margin="20,0,0,0" ItemsSource="{Binding PaginationCollection}" />
            <wd:Pagination
                Height="30"
                Margin="20"
                HorizontalAlignment="Right"
                Count="{Binding Count, Mode=TwoWay}"
                CountPerPage="{Binding CountPerPage, Mode=TwoWay}"
                Current="{Binding Current, Mode=TwoWay}"
                IsLite="False" />
        </StackPanel>
    </Grid>
</Window>

MainViewModel.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfApp5.ViewModel
{
    public class MainViewModel : ViewModelBase
    {

        private List<int> _sourceList = new List<int>();

        public MainViewModel()
        {
            _sourceList.AddRange(Enumerable.Range(1, 5));
            Count = 5;

            CurrentPageChanged();
        }

        public ObservableCollection<int> PaginationCollection { get; set; } = new ObservableCollection<int>();

        private int _count;
        public int Count
        {
            get { return _count; }
            set { _count = value; this.NotifyPropertyChange("Count"); CurrentPageChanged(); }
        }

        private int _countPerPage = 4;
        public int CountPerPage
        {
            get { return _countPerPage; }
            set { _countPerPage = value; this.NotifyPropertyChange("CountPerPage"); CurrentPageChanged(); }
        }

        private int _current = 1;
        public int Current
        {
            get { return _current; }
            set { _current = value; this.NotifyPropertyChange("Current"); CurrentPageChanged(); }
        }

        private void CurrentPageChanged()
        {
            PaginationCollection.Clear();

            foreach (var i in _sourceList.Skip((Current - 1) * CountPerPage).Take(CountPerPage))
            {
                PaginationCollection.Add(i);
            }
        }
    }
}

基本上是Demo上提供的代码,我只是修改了下Count和CountPerPage的初始值,发现Count和CountPerPage如果不能整除的话,就会出现最后一个Listboxitem点击的时候会出现这个情况,我用的是.net6版本创建的wpf空项目

麻烦提供下demo,或者把代码提供下。

MainWindow.xaml

<Window
    x:Class="WpfApp5.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:WpfApp5"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:wd="https://github.com/WPFDevelopersOrg/WPFDevelopers"
    Title="MainWindow"
    Width="800"
    Height="450"
    mc:Ignorable="d">
    <Grid>
        <StackPanel>
            <ListBox Margin="20,0,0,0" ItemsSource="{Binding PaginationCollection}" />
            <wd:Pagination
                Height="30"
                Margin="20"
                HorizontalAlignment="Right"
                Count="{Binding Count, Mode=TwoWay}"
                CountPerPage="{Binding CountPerPage, Mode=TwoWay}"
                Current="{Binding Current, Mode=TwoWay}"
                IsLite="False" />
        </StackPanel>
    </Grid>
</Window>

MainViewModel.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfApp5.ViewModel
{
    public class MainViewModel : ViewModelBase
    {

        private List<int> _sourceList = new List<int>();

        public MainViewModel()
        {
            _sourceList.AddRange(Enumerable.Range(1, 5));
            Count = 5;

            CurrentPageChanged();
        }

        public ObservableCollection<int> PaginationCollection { get; set; } = new ObservableCollection<int>();

        private int _count;
        public int Count
        {
            get { return _count; }
            set { _count = value; this.NotifyPropertyChange("Count"); CurrentPageChanged(); }
        }

        private int _countPerPage = 4;
        public int CountPerPage
        {
            get { return _countPerPage; }
            set { _countPerPage = value; this.NotifyPropertyChange("CountPerPage"); CurrentPageChanged(); }
        }

        private int _current = 1;
        public int Current
        {
            get { return _current; }
            set { _current = value; this.NotifyPropertyChange("Current"); CurrentPageChanged(); }
        }

        private void CurrentPageChanged()
        {
            PaginationCollection.Clear();

            foreach (var i in _sourceList.Skip((Current - 1) * CountPerPage).Take(CountPerPage))
            {
                PaginationCollection.Add(i);
            }
        }
    }
}

基本上是Demo上提供的代码,我只是修改了下Count和CountPerPage的初始值,发现Count和CountPerPage如果不能整除的话,就会出现最后一个Listboxitem点击的时候会出现这个情况,我用的是.net6版本创建的wpf空项目

问题我们已经定位到了,先建议这样使用

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition Height="50"/>
    </Grid.RowDefinitions>
    <ListBox Margin="20,0,0,0" ItemsSource="{Binding NormalPaginationViewModel.PaginationCollection}" />
    <wd:Pagination
        Height="30"
        Margin="20"
        Grid.Row="1"
        HorizontalAlignment="Right"
        Count="{Binding NormalPaginationViewModel.Count, Mode=TwoWay}"
        CountPerPage="{Binding NormalPaginationViewModel.CountPerPage, Mode=TwoWay}"
        Current="{Binding NormalPaginationViewModel.Current, Mode=TwoWay}"
        IsLite="False" />
</Grid>

好的好的,居然是这个原因,是真没想到= =

好的好的,居然是这个原因,是真没想到= =

下一个预览版我们会修复此问题。