基于WPF開發(fā)一個(gè)分頁控件
概要
本文將詳細(xì)介紹如何使用WPF(Windows Presentation Foundation)開發(fā)一個(gè)分頁控件,并深入解析其實(shí)現(xiàn)原理。我們將通過使用XAML和C#代碼相結(jié)合的方式構(gòu)建分頁控件,并確保它具有高度的可定制性,以便在不同的應(yīng)用場(chǎng)景中滿足各種需求。
一.簡(jiǎn)介
分頁控件是在許多應(yīng)用程序中常見的一種界面元素,特別是在數(shù)據(jù)展示的場(chǎng)景中。它允許用戶瀏覽大量數(shù)據(jù),并根據(jù)需要切換到不同的數(shù)據(jù)頁。
二.需求分析
我們首先來分析一下一個(gè)分頁控件的基本構(gòu)成。

2.1 總條目數(shù)(TotalItems)
表示總數(shù)據(jù)量。
2.2 每頁條目數(shù)(PageSize)
表示每頁顯示的條目數(shù)。
2.3 總頁數(shù)(PageCount)
表示根據(jù)總條目數(shù)與每頁條目數(shù)計(jì)算出來的頁數(shù)。
2.4 分頁/頁碼按鈕數(shù)量(PageNumberCount)
分頁控件中可以點(diǎn)擊的頁碼按鈕。
2.5 當(dāng)前頁(CurrentPage)
當(dāng)前顯示的頁,通常高亮顯示。
三.控件命令和事件
3.1 頁面跳轉(zhuǎn)命令(GoToPageCommand)
該命令用于在XAML代碼中觸發(fā)頁面跳轉(zhuǎn)操作。
3.2當(dāng)前頁變更事件
當(dāng)CurrentPage參數(shù)改變后觸發(fā)該事件,通常在該事件中執(zhí)行數(shù)據(jù)查詢操作。
四.代碼實(shí)現(xiàn)
通過以上原理分析,我們提取出了分頁控件需要包含的基本要素,下面我們通過這些信息來組裝成一個(gè)分頁控件。
<Style TargetType="{x:Type local:Pager}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:Pager}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<StackPanel Orientation="Horizontal" ClipToBounds="True">
<Button Command="{x:Static local:Pager.GoToPageCommand}" CommandParameter="1" Margin="0,0,5,0" Content="首頁"></Button>
<Button Command="{x:Static local:Pager.GoToPageCommand}" CommandParameter="-" Margin="5,0" Content="上一頁"></Button>
<ItemsControl ItemsSource="{TemplateBinding PageButtons}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ToggleButton MinWidth="{Binding RelativeSource={RelativeSource Mode=Self},Path=ActualHeight}"
Content="{Binding Page}"
IsChecked="{Binding IsCurrentPage}"
Command="{x:Static local:Pager.GoToPageCommand}"
CommandParameter="{Binding Page}"
Margin="5,0"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Button Command="{x:Static local:Pager.GoToPageCommand}" CommandParameter="+" Margin="5,0" Content="下一頁"></Button>
<Button Command="{x:Static local:Pager.GoToPageCommand}" CommandParameter="尾頁" Margin="5,0,0,0" Content="{DynamicResource LastPage}"></Button>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
五.多樣化需求
在不同的業(yè)務(wù)場(chǎng)景下需要的分頁控件可能不盡相同,那么如何來滿足多樣化需求呢,答案就是自定義控件模板。下面演示幾種常見的分頁控件如何實(shí)現(xiàn)。
只需要“上一頁”、“下一頁”的情況
<Style TargetType="{x:Type Controls:Pager}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Controls:Pager}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<StackPanel Orientation="Horizontal" ClipToBounds="True">
<Button Command="{x:Static Controls:Pager.GoToPageCommand}" CommandParameter="-" Margin="5,0" Content="{DynamicResource PreviousPage}"></Button>
<Button Command="{x:Static Controls:Pager.GoToPageCommand}" CommandParameter="+" Margin="5,0" Content="{DynamicResource NextPage}"></Button>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
只需要“首頁”、“上一頁”、“下一頁”、“尾頁”的情況。
<Style TargetType="{x:Type Controls:Pager}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Controls:Pager}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<StackPanel Orientation="Horizontal" ClipToBounds="True">
<Button Command="{x:Static Controls:Pager.GoToPageCommand}" CommandParameter="1" Margin="0,0,5,0" Content="{DynamicResource FirstPage}"></Button>
<Button Command="{x:Static Controls:Pager.GoToPageCommand}" CommandParameter="-" Margin="5,0" Content="{DynamicResource PreviousPage}"></Button>
<Button Command="{x:Static Controls:Pager.GoToPageCommand}" CommandParameter="+" Margin="5,0" Content="{DynamicResource NextPage}"></Button>
<Button Command="{x:Static Controls:Pager.GoToPageCommand}" CommandParameter="{TemplateBinding PageCount}" Margin="5,0,0,0" Content="{DynamicResource LastPage}"></Button>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
數(shù)字顯示“首頁”、“尾頁”的情況。
<Style TargetType="{x:Type Controls:Pager}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Controls:Pager}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<StackPanel Orientation="Horizontal" ClipToBounds="True">
<Button Command="{x:Static Controls:Pager.GoToPageCommand}"
CommandParameter="1"
Content="1"
MinWidth="{Binding RelativeSource={RelativeSource Mode=Self},Path=ActualHeight}"
Margin="0,0,5,0">
<Button.Visibility>
<MultiBinding Converter="{StaticResource PageNumberToVisibilityConverter}" ConverterParameter="First">
<Binding RelativeSource="{RelativeSource AncestorType=Controls:Pager}" Path="CurrentPage"/>
<Binding RelativeSource="{RelativeSource AncestorType=Controls:Pager}" Path="PageNumberCount"/>
<Binding RelativeSource="{RelativeSource AncestorType=Controls:Pager}" Path="PageCount"/>
</MultiBinding>
</Button.Visibility>
</Button>
<Button IsEnabled="False" Margin="5,0" MinWidth="{Binding RelativeSource={RelativeSource Mode=Self},Path=ActualHeight}" Content="...">
<Button.Visibility>
<MultiBinding Converter="{StaticResource PageNumberToVisibilityConverter}" ConverterParameter="First">
<Binding RelativeSource="{RelativeSource AncestorType=Controls:Pager}" Path="CurrentPage"/>
<Binding RelativeSource="{RelativeSource AncestorType=Controls:Pager}" Path="PageNumberCount"/>
<Binding RelativeSource="{RelativeSource AncestorType=Controls:Pager}" Path="PageCount"/>
</MultiBinding>
</Button.Visibility>
</Button>
<Button Command="{x:Static Controls:Pager.GoToPageCommand}" CommandParameter="-" Margin="5,0" Content="{DynamicResource PreviousPage}"></Button>
<ItemsControl ItemsSource="{TemplateBinding PageButtons}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ToggleButton MinWidth="{Binding RelativeSource={RelativeSource Mode=Self},Path=ActualHeight}"
Content="{Binding Page}"
Margin="5,0"
IsChecked="{Binding IsCurrentPage}"
Command="{x:Static Controls:Pager.GoToPageCommand}"
CommandParameter="{Binding Page}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Button Command="{x:Static Controls:Pager.GoToPageCommand}" CommandParameter="+" Margin="5,0" Content="{DynamicResource NextPage}"></Button>
<Button IsEnabled="False" Margin="5,0" MinWidth="{Binding RelativeSource={RelativeSource Mode=Self},Path=ActualHeight}" Content="...">
<Button.Visibility>
<MultiBinding Converter="{StaticResource PageNumberToVisibilityConverter}">
<Binding RelativeSource="{RelativeSource AncestorType=Controls:Pager}" Path="CurrentPage"/>
<Binding RelativeSource="{RelativeSource AncestorType=Controls:Pager}" Path="PageNumberCount"/>
<Binding RelativeSource="{RelativeSource AncestorType=Controls:Pager}" Path="PageCount"/>
</MultiBinding>
</Button.Visibility>
</Button>
<Button Command="{x:Static Controls:Pager.GoToPageCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=Controls:Pager},Path=PageCount}"
Content="{Binding RelativeSource={RelativeSource AncestorType=Controls:Pager},Path=PageCount}"
MinWidth="{Binding RelativeSource={RelativeSource Mode=Self},Path=ActualHeight}"
Margin="5,0,0,0">
<Button.Visibility>
<MultiBinding Converter="{StaticResource PageNumberToVisibilityConverter}">
<Binding RelativeSource="{RelativeSource AncestorType=Controls:Pager}" Path="CurrentPage"/>
<Binding RelativeSource="{RelativeSource AncestorType=Controls:Pager}" Path="PageNumberCount"/>
<Binding RelativeSource="{RelativeSource AncestorType=Controls:Pager}" Path="PageCount"/>
</MultiBinding>
</Button.Visibility>
</Button>
<StackPanel Orientation="Horizontal" Margin="5,0,0,0">
<TextBlock Text="轉(zhuǎn)到" VerticalAlignment="Center"/>
<TextBox x:Name="tbox_Page" Width="40" Margin="5,0" Padding="5" HorizontalContentAlignment="Center" VerticalAlignment="Center"/>
<TextBlock Text="頁" VerticalAlignment="Center"/>
<Button Margin="5,0,0,0" Command="{x:Static Controls:Pager.GoToPageCommand}" CommandParameter="{Binding ElementName=tbox_Page,Path=Text}">確定</Button>
</StackPanel>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
可以調(diào)整每頁顯示條目,可以顯示總頁數(shù),可以跳轉(zhuǎn)到指定頁的情況。
<Style TargetType="{x:Type Controls:Pager}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Controls:Pager}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<StackPanel Orientation="Horizontal" ClipToBounds="True">
<Button Command="{x:Static Controls:Pager.GoToPageCommand}" CommandParameter="1" Margin="0,0,5,0" MinWidth="{Binding RelativeSource={RelativeSource Mode=Self},Path=ActualHeight}" Content="{DynamicResource FirstPage}"></Button>
<Button Command="{x:Static Controls:Pager.GoToPageCommand}" CommandParameter="-" Margin="5,0" MinWidth="{Binding RelativeSource={RelativeSource Mode=Self},Path=ActualHeight}" Content="{DynamicResource PreviousPage}"></Button>
<ItemsControl ItemsSource="{TemplateBinding PageButtons}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ToggleButton MinWidth="{Binding RelativeSource={RelativeSource Mode=Self},Path=ActualHeight}"
Content="{Binding Page}"
Margin="5,0"
IsChecked="{Binding IsCurrentPage}"
Command="{x:Static Controls:Pager.GoToPageCommand}"
CommandParameter="{Binding Page}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Button Command="{x:Static Controls:Pager.GoToPageCommand}" CommandParameter="+" Margin="5,0" MinWidth="{Binding RelativeSource={RelativeSource Mode=Self},Path=ActualHeight}" Content="{DynamicResource NextPage}"></Button>
<Button Command="{x:Static Controls:Pager.GoToPageCommand}" CommandParameter="{TemplateBinding PageCount}" Margin="5,0,0,0" MinWidth="{Binding RelativeSource={RelativeSource Mode=Self},Path=ActualHeight}" Content="{DynamicResource LastPage}"></Button>
<StackPanel Orientation="Horizontal" Margin="5,0,0,0">
<TextBlock Margin="0,0,5,0" Text="每頁" VerticalAlignment="Center"/>
<ComboBox Margin="5,0" SelectedIndex="0" VerticalContentAlignment="Center" SelectedItem="{Binding RelativeSource={RelativeSource AncestorType=Controls:Pager},Path=PageSize,Mode=OneWayToSource}">
<sys:Int32>5</sys:Int32>
<sys:Int32>10</sys:Int32>
<sys:Int32>15</sys:Int32>
<sys:Int32>20</sys:Int32>
</ComboBox>
<TextBlock Text="條" VerticalAlignment="Center" Margin="5,0"/>
<TextBlock VerticalAlignment="Center" Margin="5,0">
<Run Text="共"/>
<Run Text="{Binding RelativeSource={RelativeSource AncestorType=Controls:Pager},Path=PageCount}"/>
<Run Text="頁"/>
</TextBlock>
<TextBlock Text="轉(zhuǎn)到" VerticalAlignment="Center"/>
<TextBox x:Name="tbox_Page" Width="40" Margin="5,0" Padding="5" HorizontalContentAlignment="Center" VerticalAlignment="Center"/>
<TextBlock Text="頁" VerticalAlignment="Center"/>
<Button Margin="5,0,0,0" Command="{x:Static Controls:Pager.GoToPageCommand}" CommandParameter="{Binding ElementName=tbox_Page,Path=Text}">確定</Button>
</StackPanel>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
除了修改模板實(shí)現(xiàn)不同的形態(tài)的分頁控件以外,還可以用圖標(biāo)替換掉“首頁”、“上一頁”、“下一頁”、”尾頁”等文字。

六.個(gè)性化控件外觀
項(xiàng)目中的界面外觀可能多種多樣,有自己寫的控件樣式,也有使用第三方UI庫的樣式,如何跟它們搭配使用呢。
自定義控件樣式
<Style TargetType="Button">
<Setter Property="Padding" Value="5"/>
<Setter Property="Background" Value="Red"/>
</Style>
<Style TargetType="ToggleButton">
<Setter Property="Padding" Value="5"/>
<Setter Property="Background" Value="Red"/>
</Style>
使用第三方UI庫
1.HandyControl
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/SkinDefault.xaml" />
<ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/Theme.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
2.MaterialDesign
以上就是基于WPF開發(fā)一個(gè)分頁控件的詳細(xì)內(nèi)容,更多關(guān)于WPF分頁控件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

以上就是基于WPF開發(fā)一個(gè)分頁控件的詳細(xì)內(nèi)容,更多關(guān)于WPF分頁控件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#實(shí)現(xiàn)求一組數(shù)據(jù)眾數(shù)的方法
這篇文章主要介紹了C#實(shí)現(xiàn)求一組數(shù)據(jù)眾數(shù)的方法,這里以浮點(diǎn)型數(shù)組為例分析了C#求眾數(shù)的算法原理與實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
C#中new和override的區(qū)別個(gè)人總結(jié)
這篇文章主要介紹了C#中new和override的區(qū)別個(gè)人總結(jié),本文以問答的方式講解了new和override的區(qū)別,需要的朋友可以參考下2015-06-06
C#注冊(cè)碼生成與驗(yàn)證機(jī)制實(shí)現(xiàn)方案
這篇文章詳細(xì)介紹了C#注冊(cè)碼生成與驗(yàn)證機(jī)制的實(shí)現(xiàn)方案,包括核心架構(gòu)設(shè)計(jì)、關(guān)鍵實(shí)現(xiàn)步驟、安全增強(qiáng)策略、擴(kuò)展功能實(shí)現(xiàn)以及測(cè)試用例設(shè)計(jì),需要的朋友可以參考下2026-03-03
基于C#的UDP協(xié)議的同步通信實(shí)現(xiàn)代碼
本篇文章主要介紹了基于C#的UDP協(xié)議的同步實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02
C#網(wǎng)絡(luò)請(qǐng)求與JSON解析的示例代碼
這篇文章主要介紹了C#網(wǎng)絡(luò)請(qǐng)求與JSON解析的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
基于WPF實(shí)現(xiàn)3D導(dǎo)航欄控件
這篇文章主要介紹了如何基于WPF實(shí)現(xiàn)簡(jiǎn)單的3D導(dǎo)航欄控件效果,文中的示例代碼講解詳細(xì),對(duì)我們的學(xué)習(xí)或工作有一定幫助,需要的小伙伴可以參考一下2024-03-03
C#代碼實(shí)現(xiàn)將Excel(XLS/XLSX)文件轉(zhuǎn)為Markdown格式
將?Excel?文件轉(zhuǎn)換為?Markdown,可以方便地將結(jié)構(gòu)化數(shù)據(jù)嵌入技術(shù)文檔,本文將介紹如何使用?C#?結(jié)合Spire.XLS?for?.NET庫,編程實(shí)現(xiàn)?Excel?文件到?Markdown?格式的轉(zhuǎn)換,有需要的可以了解下2026-03-03

