WPF分页控件

1.XML

<UserControl x:Class="MahAppsMetro.Demo.Views.DataPager"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:MahAppsMetro.Demo.Views"
             mc:Ignorable="d"  d:DesignWidth="300">
    <UserControl.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="VerticalAlignment" Value="Center" />
        </Style>
    </UserControl.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <!--首页-->
            <ColumnDefinition Width="Auto" />
            <!--上一页-->
            <ColumnDefinition />
            <!--页码-->
            <ColumnDefinition Width="Auto" />
            <!--下一页-->
            <ColumnDefinition Width="Auto" />
            <!--末页-->
        </Grid.ColumnDefinitions>

        <TextBlock Margin="5"><Hyperlink x:Name="PART_FirstPage">首页</Hyperlink></TextBlock>
        <TextBlock Margin="5" Grid.Column="1"><Hyperlink x:Name="PART_PreviePage">上一页</Hyperlink></TextBlock>

        <ItemsControl x:Name="PART_PageCodes" Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Center">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <!--<ItemsControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock Margin="5" Text="{Binding Mode=OneWay}"></TextBlock>
                </DataTemplate>
            </ItemsControl.ItemTemplate>-->
        </ItemsControl>


        <TextBlock Margin="5" Grid.Column="3"><Hyperlink x:Name="PART_NextPage">下一页</Hyperlink></TextBlock>
        <TextBlock Margin="5" Grid.Column="4"><Hyperlink x:Name="PART_LastPage">末页</Hyperlink></TextBlock>
    </Grid>
</UserControl>

2:C#

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;

namespace MahAppsMetro.Demo.Views
{
    /// <summary>
    /// Interaction logic for DataPager.xaml
    /// </summary>
    public partial class DataPager : UserControl
    {
        public int PageCount
        {
            get { return (int)GetValue(PageCountProperty); }
            set { SetValue(PageCountProperty, value); }
        }

        // Using a DependencyProperty as the backing store for PageCount.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PageCountProperty =
            DependencyProperty.Register("PageCount", typeof(int), typeof(DataPager), new PropertyMetadata(1, PageCountChanged));

        private static void PageCountChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            int oldValue = Convert.ToInt32(e.OldValue);
            int newValue = Convert.ToInt32(e.NewValue);
            DataPager pager = (DataPager)d;

            if (newValue <= 0) return;
            pager.ResetList();
        }

        public int Value
        {
            get { return (int)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Value.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(int), typeof(DataPager), new PropertyMetadata(1, OnValueChanged));


        public ICommand NavigateCommand
        {
            get { return (ICommand)GetValue(NavigateCommandProperty); }
            set { SetValue(NavigateCommandProperty, value); }
        }

        // Using a DependencyProperty as the backing store for NavigateCommand.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty NavigateCommandProperty =
            DependencyProperty.Register("NavigateCommand", typeof(ICommand), typeof(DataPager));

        public DataPager()
        {
            InitializeComponent();
            this.PART_FirstPage.Click += (s, e) => Value = 1;
            this.PART_LastPage.Click += (s, e) => Value = PageCount;
            this.PART_PreviePage.Click += (s, e) => Value--;
            this.PART_NextPage.Click += (s, e) => Value++;
        }

        private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            int oldValue = Convert.ToInt32(e.OldValue);
            int newValue = Convert.ToInt32(e.NewValue);
            DataPager pager = (DataPager)d;

            pager.ResetList();
            pager.NavigateCommand?.Execute(newValue);
        }

        private void ResetList()
        {
            PART_FirstPage.IsEnabled = true;
            PART_PreviePage.IsEnabled = true;
            PART_LastPage.IsEnabled = true;
            PART_NextPage.IsEnabled = true;
            if (Value == 1)
            {
                PART_FirstPage.IsEnabled = false;
                PART_PreviePage.IsEnabled = false;
            }
            if (Value >= PageCount)
            {
                PART_LastPage.IsEnabled = false;
                PART_NextPage.IsEnabled = false;
            }

            List<FrameworkElement> list = new List<FrameworkElement>();
            if(Value > 10)
                list.Add(new TextBlock(new Run("...")) { Margin = new Thickness(5) });

            int startIndex = Value -  10 >= 0 ? Value - 10 + 1 : 1;
            for (int i = startIndex; i <= PageCount && i < startIndex + 10; i++)
            {
                if (Value == i)
                    list.Add(new TextBlock(new Run(i.ToString())) { Margin = new Thickness(5) });
                else
                {
                    Hyperlink link = new Hyperlink(new Run(i.ToString())) { Tag = i };
                    link.Click += (s,e)=> Value = Convert.ToInt32(link.Tag);
                    list.Add(new TextBlock(link) { Margin = new Thickness(5) });
                }
            }

            if (PageCount > 10 && Value < PageCount)
                list.Add(new TextBlock(new Run("...")) { Margin = new Thickness(5) });
            this.PART_PageCodes.ItemsSource = list;
        }
    }
}

 

转载于:https://www.cnblogs.com/RedSky/p/10984877.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值