您的位置:首页 > 其它

WPF 自定义分页控件二

2018-09-14 11:00 417 查看

一:添加自定义分页控件,命名为KDataPagerTwo:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:BaseControl"
>
<Style TargetType="Image" x:Key="Img_Size" >
<Setter Property="Width" Value="14"/>
<Setter Property="Height" Value="14"/>
</Style>
<Style TargetType="TextBlock" x:Key="Txt_Root">
<Setter Property="FontFamily" Value="新宋体"></Setter>
<Setter Property="FontSize" Value="14"></Setter>
<Setter Property="VerticalAlignment" Value="Center"></Setter>
<Setter Property="Foreground" Value="#193A57"></Setter>
</Style>
<Style TargetType="TextBlock" x:Key="Txt_Side" BasedOn="{StaticResource Txt_Root}">
<Setter Property="Foreground" Value="#193A57"></Setter>
<Setter Property="HorizontalAlignment" Value="Center"></Setter>
</Style>
<Style TargetType="TextBlock" x:Key="Txt_Margin" BasedOn="{StaticResource Txt_Root}">
<Setter Property="Margin" Value="4"/>
<Setter Property="Cursor" Value="Hand"></Setter>
</Style>
<Style TargetType="local:KImgButton">
<Setter Property="IsEnabled" Value="True"></Setter>
<Setter Property="CornerRadius" Value="2"></Setter>
<Setter Property="FIconSize" Value="0"></Setter>
</Style>
<Style TargetType="{x:Type local:KDataPagerTwo}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:KDataPagerTwo}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackPanel Orientation="Horizontal" Margin="10,0">
<TextBlock Style="{StaticResource Txt_Side}" Text="共"></TextBlock>
<TextBlock Style="{StaticResource Txt_Side}" Text="0" Margin="5,0"  x:Name="txt_Total"></TextBlock>
<TextBlock Style="{StaticResource Txt_Side}" Text="条数据。"></TextBlock>
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Column="1" x:Name="Stack_Control" HorizontalAlignment="Right" Margin="10,0">
<Image x:Name="Img_HomePage" Source="/BaseControl;component/Images/DataPagerImages/First.png" Margin="5,0" Style="{ StaticResource Img_Size}" Cursor="Hand"/>
<Image x:Name="Img_PreviousPage" Source="/BaseControl;component/Images/DataPagerImages/prev.png" Style="{ StaticResource Img_Size}" Cursor="Hand"/>

<TextBlock  x:Name="txt_One" Text="1" Style="{StaticResource Txt_Margin}"/>
<TextBlock  x:Name="txt_Two" Text="2" Style="{StaticResource Txt_Margin}"/>
<TextBlock  x:Name="txt_Three" Text="3" Style="{StaticResource Txt_Margin}"/>
<TextBlock  x:Name="txt_Four" Text="4" Style="{StaticResource Txt_Margin}"/>
<TextBlock  x:Name="txt_Five" Text="●●●" Style="{StaticResource Txt_Margin}"/>
<TextBlock  x:Name="txt_Six" Text="10" Style="{StaticResource Txt_Margin}"/>

<Image x:Name="Img_NextPage" Source="/BaseControl;component/Images/DataPagerImages/Next.png" Style="{ StaticResource Img_Size}" Cursor="Hand"/>
<Image x:Name="Img_TailPage" Source="/BaseControl;component/Images/DataPagerImages/Last.png" Margin="5,0,20,0"  Style="{ StaticResource Img_Size}" Cursor="Hand"/>

<TextBlock Text="到第" Style="{StaticResource Txt_Root}" Margin="-5,5,5,5"></TextBlock>

<Border Margin="0,5,5,5" Background="#4081D1" BorderBrush="#4081D1" Width="19" Height="19">
<TextBox x:Name="txt_Jump" FontFamily="微软雅黑" VerticalContentAlignment="Center" VerticalAlignment="Center" HorizontalContentAlignment="Center" HorizontalAlignment="Center" FontSize="12" Width="17" Height="17" BorderThickness="0"/>
</Border>

<TextBlock Text="页" Style="{StaticResource Txt_Root}" Margin="0,0,20,0"></TextBlock>

<local:KImgButton x:Name="btn_Ok" FontFamily="新宋体" FontSize="14" Content="跳转" Height="20" Width="50" Background="#4081D1" Margin="-10,0,0,0"/>
</StackPanel>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
View Code

三:在Generic中引用资源字典文件:

<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/BaseControl;component/Themes/DataPagerTwo.xaml" />
</ResourceDictionary.MergedDictionaries>

四:将Generic中下面代码删除:

<Style TargetType="{x:Type local:KDataPagerTwo}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:KDataPagerTwo}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

五:现在就可以用了,用法:

先引用:

再添加:

 六:后台PropertyChanged事件代码(当前页数发生变化,就可以根据DataPager.PagerIndex传入分页方法):

/// <summary>
/// 当某一属性值发生改变事件
/// </summary>
private void DataPager_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
//分页方法(根据需要可以写成其他分页方法)
       //DataPager.PagerIndex--当前页,DataPager.PagerSize--每页记录数 datagrid1.ItemsSource = TL.ToList().Skip((DataPager.PagerIndex - 1) * DataPager.PagerSize).Take(DataPager.PagerSize).ToList(); }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: