WPF 自定义分页控件二

时间:2023-03-08 16:37:52

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

 public class KDataPagerTwo : Control, INotifyPropertyChanged
{
static KDataPagerTwo()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(KDataPagerTwo), new FrameworkPropertyMetadata(typeof(KDataPagerTwo)));
} public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
if (propertyName == "PagerIndex")
{
if (PagerIndex != )
ChosenNumber();
}
else if (propertyName == "PagerTotal" || propertyName == "PagerSize")
Refresh();
} #region 变量定义
public TextBox txt_Jump;
public TextBlock txt_One;
public TextBlock txt_Two;
public TextBlock txt_Three;
public TextBlock txt_Four;
public TextBlock txt_Five;
public TextBlock txt_Six;
public TextBlock txt_Total;
/// <summary>
/// 首页
/// </summary>
public Image Img_HomePage;
/// <summary>
/// 上一页
/// </summary>
public Image Img_PreviousPage;
/// <summary>
/// 下一页
/// </summary>
public Image Img_NextPage;
/// <summary>
/// 尾页
/// </summary>
public Image Img_TailPage;
/// <summary>
/// 跳转按钮
/// </summary>
public Button btn_Ok;
#endregion #region 依赖属性
/// <summary>
/// 页大小
/// </summary>
public int PagerSize
{
get { return (int)GetValue(PagerSizeProperty); }
set { SetValue(PagerSizeProperty, value); OnPropertyChanged("PagerSize"); }
} /// <summary>
/// 当前页
/// </summary>
public int PagerIndex
{
get { return (int)GetValue(PagerIndexProperty); }
set { SetValue(PagerIndexProperty, value); OnPropertyChanged("PagerIndex"); }
} /// <summary>
/// 总计录数
/// </summary>
public int PagerTotal
{
get { return (int)GetValue(PagerTotalProperty); }
set { SetValue(PagerTotalProperty, value); OnPropertyChanged("PagerTotal"); }
} /// <summary>
/// 总页数
/// </summary>
public int PagerCount
{
get { return (int)GetValue(PagerCountProperty); }
set { SetValue(PagerCountProperty, value); OnPropertyChanged("PagerCount"); }
} //使用一个依赖属性作为PagerCount的后备存储器。这支持动画、样式、绑定等。
public static readonly DependencyProperty PagerCountProperty =
DependencyProperty.Register("PagerCount", typeof(int), typeof(KDataPagerTwo), new UIPropertyMetadata()); //使用一个可靠的属性作为总的后备存储器。这支持动画、样式、绑定等
public static readonly DependencyProperty PagerTotalProperty =
DependencyProperty.Register("PagerTotal", typeof(int), typeof(KDataPagerTwo), new UIPropertyMetadata()); //使用一个依赖属性作为PagerIndex的后备存储器。这支持动画、样式、绑定等。
public static readonly DependencyProperty PagerIndexProperty =
DependencyProperty.Register("PagerIndex", typeof(int), typeof(KDataPagerTwo), new UIPropertyMetadata()); //使用一个依赖属性作为PagerSize的后备存储器。这支持动画、样式、绑定等。
public static readonly DependencyProperty PagerSizeProperty =
DependencyProperty.Register("PagerSize", typeof(int), typeof(KDataPagerTwo), new UIPropertyMetadata());
#endregion 依赖属性_end public override void OnApplyTemplate()
{
base.OnApplyTemplate(); Img_HomePage = GetTemplateChild("Img_HomePage") as Image;
Img_PreviousPage = GetTemplateChild("Img_PreviousPage") as Image;
Img_NextPage = GetTemplateChild("Img_NextPage") as Image;
Img_TailPage = GetTemplateChild("Img_TailPage") as Image;
btn_Ok = GetTemplateChild("btn_Ok") as Button;
txt_Jump = GetTemplateChild("txt_Jump") as TextBox; txt_One = GetTemplateChild("txt_One") as TextBlock;
txt_Two = GetTemplateChild("txt_Two") as TextBlock;
txt_Three = GetTemplateChild("txt_Three") as TextBlock;
txt_Four = GetTemplateChild("txt_Four") as TextBlock;
txt_Five = GetTemplateChild("txt_Five") as TextBlock;
txt_Six = GetTemplateChild("txt_Six") as TextBlock;
txt_Total = GetTemplateChild("txt_Total") as TextBlock;
// 绑定事件
Img_HomePage.MouseLeftButtonUp += Img_HomePage_MouseLeftButtonUp;
Img_PreviousPage.MouseLeftButtonUp += Img_PreviousPage_MouseLeftButtonUp;
Img_NextPage.MouseLeftButtonUp += Img_NextPage_MouseLeftButtonUp;
Img_TailPage.MouseLeftButtonUp += Img_TailPage_MouseLeftButtonUp;
btn_Ok.Click += Btn_Ok_Click; ;
txt_Jump.TextChanged += Txt_Jump_TextChanged; txt_One.MouseLeftButtonUp += Txt_MouseLeftButtonUp;
txt_Two.MouseLeftButtonUp += Txt_MouseLeftButtonUp;
txt_Three.MouseLeftButtonUp += Txt_MouseLeftButtonUp;
txt_Four.MouseLeftButtonUp += Txt_MouseLeftButtonUp;
txt_Five.MouseLeftButtonUp += Txt_MouseLeftButtonUp;
txt_Six.MouseLeftButtonUp += Txt_MouseLeftButtonUp;
//刷新页数
Refresh();
} /// <summary>
/// 点击TextBlock事件
/// </summary>
private void Txt_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if ((sender as TextBlock).Text.ToString() != "…")
PagerIndex = int.Parse((sender as TextBlock).Text);
} /// <summary>
/// 只能输入数字
/// </summary>
private void Txt_Jump_TextChanged(object sender, TextChangedEventArgs e)
{
//屏蔽中文输入和非法字符粘贴输入
TextBox textBox = sender as TextBox;
TextChange[] change = new TextChange[e.Changes.Count];
e.Changes.CopyTo(change, );
int offset = change[].Offset;
if (change[].AddedLength > )
{
double num = ;
if (!Double.TryParse(textBox.Text, out num))
{
textBox.Text = textBox.Text.Remove(offset, change[].AddedLength);
textBox.Select(offset, );
}
}
} /// <summary>
/// 跳转
/// </summary>
private void Btn_Ok_Click(object sender, RoutedEventArgs e)
{
int txt = int.Parse(txt_Jump.Text.ToString() == "" ? "" : txt_Jump.Text.ToString());
if (txt > && txt <= PagerCount)
PagerIndex = txt;
} /// <summary>
/// 首页
/// </summary>
private void Img_HomePage_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
PagerIndex = ;
} /// <summary>
/// 尾页
/// </summary>
private void Img_TailPage_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
PagerIndex = PagerCount;
} /// <summary>
/// 上一页
/// </summary>
private void Img_PreviousPage_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (PagerIndex > )
PagerIndex--;
} /// <summary>
/// 下一页
/// </summary>
private void Img_NextPage_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (PagerIndex < PagerCount)
PagerIndex++;
} #region 方法 /// <summary>
/// 选中数字的样式
/// </summary>
public void ChosenNumber()
{
if (PagerIndex > (PagerCount - ))
{
ColorChanged( - (PagerCount - PagerIndex)); LatterNumberChanged(PagerCount);
}
else
{
ColorChanged();
ForeFiveNumberChanged(PagerIndex);
LatterTwo();
}
} SolidColorBrush ScbBlue = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#193A57"));//蓝色
SolidColorBrush ScbRed = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#E92F2F"));//红色 /// <summary>
/// 当前页变为红色
/// </summary>
public void ColorChanged(int GOTO)
{
txt_One.Foreground = ScbBlue;
txt_Two.Foreground = ScbBlue;
txt_Three.Foreground = ScbBlue;
txt_Four.Foreground = ScbBlue;
txt_Five.Foreground = ScbBlue;
txt_Six.Foreground = ScbBlue;
switch (GOTO)
{
case :
goto GT1;
case :
goto GT2;
case :
goto GT3;
case :
goto GT4;
case :
goto GT5;
case :
goto GT6;
}
GT1: txt_One.Foreground = ScbRed;
return;
GT2: txt_Two.Foreground = ScbRed;
return;
GT3: txt_Three.Foreground = ScbRed;
return;
GT4: txt_Four.Foreground = ScbRed;
return;
GT5: txt_Five.Foreground = ScbRed;
return;
GT6: txt_Six.Foreground = ScbRed;
} /// <summary>
/// 前四个数字变化
/// </summary>
/// <param name="InitialNumber">开始数字</param>
public void ForeFiveNumberChanged(int InitialNumber)
{
txt_One.Text = InitialNumber.ToString();
txt_Two.Text = (InitialNumber + ).ToString();
txt_Three.Text = (InitialNumber + ).ToString();
txt_Four.Text = (InitialNumber + ).ToString();
} /// <summary>
/// 设置后两位数字
/// </summary>
public void LatterTwo()
{
txt_Six.Text = PagerCount.ToString();
if (PagerCount > )
txt_Five.Text = "…";
else
txt_Five.Text = (PagerCount - ).ToString();
} /// <summary>
/// 数字从尾数开始变化
/// </summary>
/// <param name="Mantissa">尾数</param>
public void LatterNumberChanged(int Mantissa)
{
txt_Six.Text = Mantissa.ToString();
txt_Five.Text = (Mantissa - ).ToString();
txt_Four.Text = (Mantissa - ).ToString();
txt_Three.Text = (Mantissa - ).ToString();
txt_Two.Text = (Mantissa - ).ToString();
txt_One.Text = (Mantissa - ).ToString();
} /// <summary>
/// 设置总页数
/// </summary>
public void SetPagerCount()
{
int pc = PagerTotal / PagerSize;
if (PagerTotal % PagerSize == )
PagerCount = pc;
else
PagerCount = pc + ;
if (PagerCount <= )
CollapsedTXT(PagerCount);
txt_Total.Text = PagerTotal.ToString();
} /// <summary>
/// 小于6页的隐藏部分控件
/// </summary>
/// <param name="CollapsedStartTXT">从第几个开始</param>
public void CollapsedTXT(int CollapsedStartTXT)
{
switch (CollapsedStartTXT)
{
case :
goto CST1;
case :
goto CST2;
case :
goto CST3;
case :
goto CST4;
case :
goto CST5;
}
return;
CST1: txt_Two.Visibility = Visibility.Collapsed;
CST2: txt_Three.Visibility = Visibility.Collapsed;
CST3: txt_Four.Visibility = Visibility.Collapsed;
CST4: txt_Five.Visibility = Visibility.Collapsed;
CST5: txt_Six.Visibility = Visibility.Collapsed;
} /// <summary>
/// 刷新
/// </summary>
public void Refresh()
{
SetPagerCount();
ForeFiveNumberChanged(PagerIndex);
ColorChanged(PagerIndex);
LatterTwo();
}
#endregion
}

二:定义资源字典文件,命名为DataPagerTwo:

说明:local:KImgButton,这个也是一个自定义控件,可以改成Button控件也没有问题

<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>

三:在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>

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

先引用:WPF 自定义分页控件二

再添加:WPF 自定义分页控件二

六:后台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 - ) * DataPager.PagerSize).Take(DataPager.PagerSize).ToList();
}