欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于WPF開發(fā)一個分頁控件

 更新時間:2024年04月03日 11:38:26   作者:趨時軟件  
這篇文章主要為大家詳細介紹了如何使用WPF(Windows?Presentation?Foundation)開發(fā)一個分頁控件,并深入解析其實現(xiàn)原理,感興趣的小伙伴可以了解下

概要

本文將詳細介紹如何使用WPF(Windows Presentation Foundation)開發(fā)一個分頁控件,并深入解析其實現(xiàn)原理。我們將通過使用XAML和C#代碼相結(jié)合的方式構(gòu)建分頁控件,并確保它具有高度的可定制性,以便在不同的應用場景中滿足各種需求。

一.簡介

分頁控件是在許多應用程序中常見的一種界面元素,特別是在數(shù)據(jù)展示的場景中。它允許用戶瀏覽大量數(shù)據(jù),并根據(jù)需要切換到不同的數(shù)據(jù)頁。

二.需求分析

我們首先來分析一下一個分頁控件的基本構(gòu)成。

2.1 總條目數(shù)(TotalItems)

表示總數(shù)據(jù)量。

2.2 每頁條目數(shù)(PageSize)

表示每頁顯示的條目數(shù)。

2.3 總頁數(shù)(PageCount)

表示根據(jù)總條目數(shù)與每頁條目數(shù)計算出來的頁數(shù)。

2.4 分頁/頁碼按鈕數(shù)量(PageNumberCount)

分頁控件中可以點擊的頁碼按鈕。

2.5 當前頁(CurrentPage)

當前顯示的頁,通常高亮顯示。

三.控件命令和事件

3.1 頁面跳轉(zhuǎn)命令(GoToPageCommand)

該命令用于在XAML代碼中觸發(fā)頁面跳轉(zhuǎn)操作。

3.2當前頁變更事件

當CurrentPage參數(shù)改變后觸發(fā)該事件,通常在該事件中執(zhí)行數(shù)據(jù)查詢操作。

四.代碼實現(xiàn)

通過以上原理分析,我們提取出了分頁控件需要包含的基本要素,下面我們通過這些信息來組裝成一個分頁控件。

<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è)務場景下需要的分頁控件可能不盡相同,那么如何來滿足多樣化需求呢,答案就是自定義控件模板。下面演示幾種常見的分頁控件如何實現(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>

除了修改模板實現(xiàn)不同的形態(tài)的分頁控件以外,還可以用圖標替換掉“首頁”、“上一頁”、“下一頁”、”尾頁”等文字。

六.個性化控件外觀

項目中的界面外觀可能多種多樣,有自己寫的控件樣式,也有使用第三方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ā)一個分頁控件的詳細內(nèi)容,更多關于WPF分頁控件的資料請關注腳本之家其它相關文章!

以上就是基于WPF開發(fā)一個分頁控件的詳細內(nèi)容,更多關于WPF分頁控件的資料請關注腳本之家其它相關文章!

相關文章

  • C#代碼實現(xiàn)短信驗證碼接口示例

    C#代碼實現(xiàn)短信驗證碼接口示例

    這篇文章主要為大家詳細介紹了C#實現(xiàn)短信驗證碼接口示例代碼,感興趣的小伙伴們可以參考一下
    2016-08-08
  • 使用C#在PDF中添加和刪除水印注釋

    使用C#在PDF中添加和刪除水印注釋

    PDF中的水印注釋是一種獨特的注釋類型,它通常以透明的文本或圖片形式疊加在頁面內(nèi)容之上,為文檔添加標識或信息提示,這篇博客將探討如何使用C# 在PDF文檔中添加和刪除水印注釋,感興趣的小伙伴跟著小編一起來看看吧
    2025-02-02
  • 利用C#實現(xiàn)獲取當前設備硬件信息

    利用C#實現(xiàn)獲取當前設備硬件信息

    這篇文章主要為大家詳細介紹了如何利用C#實現(xiàn)獲取當前設備硬件信息的功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起了解一下
    2023-03-03
  • C# SqlSugar批量執(zhí)行SQL語句及批量更新實體對象的操作方法

    C# SqlSugar批量執(zhí)行SQL語句及批量更新實體對象的操作方法

    SqlSugar 是一款 老牌 .NET開源ORM框架,由果糖大數(shù)據(jù)科技團隊維護和更新 ,開箱即用最易上手的ORM,這篇文章主要介紹了C# SqlSugar批量執(zhí)行SQL語句以及批量更新實體對象,需要的朋友可以參考下
    2024-07-07
  • C#環(huán)形隊列的實現(xiàn)方法詳解

    C#環(huán)形隊列的實現(xiàn)方法詳解

    這篇文章先是簡單的給大家介紹了什么是環(huán)形隊列和環(huán)形隊列的優(yōu)點,然后通過實例代碼給大家介紹C#如何實現(xiàn)環(huán)形隊列,有需要的朋友們可以參考借鑒,下面來一起看看吧。
    2016-09-09
  • 使用C#實現(xiàn)讀取PDF中所有文本內(nèi)容

    使用C#實現(xiàn)讀取PDF中所有文本內(nèi)容

    這篇文章主要為大家詳細介紹了如何使用C#實現(xiàn)讀取PDF中所有文本內(nèi)容,文中的示例代碼簡潔易懂,具有一定的學習價值,有需要的小伙伴可以了解下
    2024-02-02
  • C#類中static變量用法分析

    C#類中static變量用法分析

    這篇文章主要介紹了C#類中static變量用法,實例分析了static變量使用技巧與相關注意事項,需要的朋友可以參考下
    2015-01-01
  • C#使用Socket實現(xiàn)本地多人聊天室

    C#使用Socket實現(xiàn)本地多人聊天室

    這篇文章主要為大家詳細介紹了C#使用Socket實現(xiàn)本地多人聊天室,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • 利用C#/VB.NET實現(xiàn)將PDF轉(zhuǎn)為Word

    利用C#/VB.NET實現(xiàn)將PDF轉(zhuǎn)為Word

    眾所周知,PDF 文檔支持特長文件,集成度和安全可靠性都較高,可有效防止他人對 PDF 內(nèi)容進行更改,所以在工作中深受大家喜愛。本文將分為兩部分介紹如何以編程的方式將 PDF 轉(zhuǎn)換為 Word,需要的可以參考一下
    2022-12-12
  • C#語言主要特性總結(jié)

    C#語言主要特性總結(jié)

    這篇文章主要介紹了C#語言主要特性總結(jié),本文總結(jié)了C#語言的簡單、現(xiàn)代、面向?qū)ο?、類型安全、相互兼容性、可伸縮性和可升級性等幾個主要特點,需要的朋友可以參考下
    2015-02-02

最新評論