一:添加自定义分页控件,命名为KDataPagerTwo:
1 public class KDataPagerTwo : Control, INotifyPropertyChanged 2 { 3 static KDataPagerTwo() 4 { 5 DefaultStyleKeyProperty.OverrideMetadata(typeof(KDataPagerTwo), new FrameworkPropertyMetadata(typeof(KDataPagerTwo))); 6 } 7 8 public event PropertyChangedEventHandler PropertyChanged; 9 protected void OnPropertyChanged(string propertyName) 10 { 11 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 12 if (propertyName == "PagerIndex") 13 { 14 if (PagerIndex != 0) 15 ChosenNumber(); 16 } 17 else if (propertyName == "PagerTotal" || propertyName == "PagerSize") 18 Refresh(); 19 } 20 21 #region 变量定义 22 public TextBox txt_Jump; 23 public TextBlock txt_One; 24 public TextBlock txt_Two; 25 public TextBlock txt_Three; 26 public TextBlock txt_Four; 27 public TextBlock txt_Five; 28 public TextBlock txt_Six; 29 public TextBlock txt_Total; 30 /// <summary> 31 /// 首页 32 /// </summary> 33 public Image Img_HomePage; 34 /// <summary> 35 /// 上一页 36 /// </summary> 37 public Image Img_PreviousPage; 38 /// <summary> 39 /// 下一页 40 /// </summary> 41 public Image Img_NextPage; 42 /// <summary> 43 /// 尾页 44 /// </summary> 45 public Image Img_TailPage; 46 /// <summary> 47 /// 跳转按钮 48 /// </summary> 49 public Button btn_Ok; 50 #endregion 51 52 #region 依赖属性 53 /// <summary> 54 /// 页大小 55 /// </summary> 56 public int PagerSize 57 { 58 get { return (int)GetValue(PagerSizeProperty); } 59 set { SetValue(PagerSizeProperty, value); OnPropertyChanged("PagerSize"); } 60 } 61 62 /// <summary> 63 /// 当前页 64 /// </summary> 65 public int PagerIndex 66 { 67 get { return (int)GetValue(PagerIndexProperty); } 68 set { SetValue(PagerIndexProperty, value); OnPropertyChanged("PagerIndex"); } 69 } 70 71 /// <summary> 72 /// 总计录数 73 /// </summary> 74 public int PagerTotal 75 { 76 get { return (int)GetValue(PagerTotalProperty); } 77 set { SetValue(PagerTotalProperty, value); OnPropertyChanged("PagerTotal"); } 78 } 79 80 /// <summary> 81 /// 总页数 82 /// </summary> 83 public int PagerCount 84 { 85 get { return (int)GetValue(PagerCountProperty); } 86 set { SetValue(PagerCountProperty, value); OnPropertyChanged("PagerCount"); } 87 } 88 89 //使用一个依赖属性作为PagerCount的后备存储器。这支持动画、样式、绑定等。 90 public static readonly DependencyProperty PagerCountProperty = 91 DependencyProperty.Register("PagerCount", typeof(int), typeof(KDataPagerTwo), new UIPropertyMetadata(15)); 92 93 //使用一个可靠的属性作为总的后备存储器。这支持动画、样式、绑定等 94 public static readonly DependencyProperty PagerTotalProperty = 95 DependencyProperty.Register("PagerTotal", typeof(int), typeof(KDataPagerTwo), new UIPropertyMetadata(150)); 96 97 98 //使用一个依赖属性作为PagerIndex的后备存储器。这支持动画、样式、绑定等。 99 public static readonly DependencyProperty PagerIndexProperty = 100 DependencyProperty.Register("PagerIndex", typeof(int), typeof(KDataPagerTwo), new UIPropertyMetadata(1)); 101 102 103 //使用一个依赖属性作为PagerSize的后备存储器。这支持动画、样式、绑定等。 104 public static readonly DependencyProperty PagerSizeProperty = 105 DependencyProperty.Register("PagerSize", typeof(int), typeof(KDataPagerTwo), new UIPropertyMetadata(10)); 106 #endregion 依赖属性_end 107 108 public override void OnApplyTemplate() 109 { 110 base.OnApplyTemplate(); 111 112 Img_HomePage = GetTemplateChild("Img_HomePage") as Image; 113 Img_PreviousPage = GetTemplateChild("Img_PreviousPage") as Image; 114 Img_NextPage = GetTemplateChild("Img_NextPage") as Image; 115 Img_TailPage = GetTemplateChild("Img_TailPage") as Image; 116 btn_Ok = GetTemplateChild("btn_Ok") as Button; 117 txt_Jump = GetTemplateChild("txt_Jump") as TextBox; 118 119 txt_One = GetTemplateChild("txt_One") as TextBlock; 120 txt_Two = GetTemplateChild("txt_Two") as TextBlock; 121 txt_Three = GetTemplateChild("txt_Three") as TextBlock; 122 txt_Four = GetTemplateChild("txt_Four") as TextBlock; 123 txt_Five = GetTemplateChild("txt_Five") as TextBlock; 124 txt_Six = GetTemplateChild("txt_Six") as TextBlock; 125 txt_Total = GetTemplateChild("txt_Total") as TextBlock; 126 // 绑定事件 127 Img_HomePage.MouseLeftButtonUp += Img_HomePage_MouseLeftButtonUp; 128 Img_PreviousPage.MouseLeftButtonUp += Img_PreviousPage_MouseLeftButtonUp; 129 Img_NextPage.MouseLeftButtonUp += Img_NextPage_MouseLeftButtonUp; 130 Img_TailPage.MouseLeftButtonUp += Img_TailPage_MouseLeftButtonUp; 131 btn_Ok.Click += Btn_Ok_Click; ; 132 txt_Jump.TextChanged += Txt_Jump_TextChanged; 133 134 txt_One.MouseLeftButtonUp += Txt_MouseLeftButtonUp; 135 txt_Two.MouseLeftButtonUp += Txt_MouseLeftButtonUp; 136 txt_Three.MouseLeftButtonUp += Txt_MouseLeftButtonUp; 137 txt_Four.MouseLeftButtonUp += Txt_MouseLeftButtonUp; 138 txt_Five.MouseLeftButtonUp += Txt_MouseLeftButtonUp; 139 txt_Six.MouseLeftButtonUp += Txt_MouseLeftButtonUp; 140 //刷新页数 141 Refresh(); 142 } 143 144 /// <summary> 145 /// 点击TextBlock事件 146 /// </summary> 147 private void Txt_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) 148 { 149 if ((sender as TextBlock).Text.ToString() != "…") 150 PagerIndex = int.Parse((sender as TextBlock).Text); 151 } 152 153 /// <summary> 154 /// 只能输入数字 155 /// </summary> 156 private void Txt_Jump_TextChanged(object sender, TextChangedEventArgs e) 157 { 158 //屏蔽中文输入和非法字符粘贴输入 159 TextBox textBox = sender as TextBox; 160 TextChange[] change = new TextChange[e.Changes.Count]; 161 e.Changes.CopyTo(change, 0); 162 int offset = change[0].Offset; 163 if (change[0].AddedLength > 0) 164 { 165 double num = 0; 166 if (!Double.TryParse(textBox.Text, out num)) 167 { 168 textBox.Text = textBox.Text.Remove(offset, change[0].AddedLength); 169 textBox.Select(offset, 0); 170 } 171 } 172 } 173 174 /// <summary> 175 /// 跳转 176 /// </summary> 177 private void Btn_Ok_Click(object sender, RoutedEventArgs e) 178 { 179 int txt = int.Parse(txt_Jump.Text.ToString() == "" ? "0" : txt_Jump.Text.ToString()); 180 if (txt > 0 && txt <= PagerCount) 181 PagerIndex = txt; 182 } 183 184 /// <summary> 185 /// 首页 186 /// </summary> 187 private void Img_HomePage_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) 188 { 189 PagerIndex = 1; 190 } 191 192 /// <summary> 193 /// 尾页 194 /// </summary> 195 private void Img_TailPage_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) 196 { 197 PagerIndex = PagerCount; 198 } 199 200 /// <summary> 201 /// 上一页 202 /// </summary> 203 private void Img_PreviousPage_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) 204 { 205 if (PagerIndex > 1) 206 PagerIndex--; 207 } 208 209 /// <summary> 210 /// 下一页 211 /// </summary> 212 private void Img_NextPage_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) 213 { 214 if (PagerIndex < PagerCount) 215 PagerIndex++; 216 } 217 218 #region 方法 219 220 /// <summary> 221 /// 选中数字的样式 222 /// </summary> 223 public void ChosenNumber() 224 { 225 if (PagerIndex > (PagerCount - 6)) 226 { 227 ColorChanged(6 - (PagerCount - PagerIndex)); LatterNumberChanged(PagerCount); 228 } 229 else 230 { 231 ColorChanged(1); 232 ForeFiveNumberChanged(PagerIndex); 233 LatterTwo(); 234 } 235 } 236 237 SolidColorBrush ScbBlue = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#193A57"));//蓝色 238 SolidColorBrush ScbRed = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#E92F2F"));//红色 239 240 /// <summary> 241 /// 当前页变为红色 242 /// </summary> 243 public void ColorChanged(int GOTO) 244 { 245 txt_One.Foreground = ScbBlue; 246 txt_Two.Foreground = ScbBlue; 247 txt_Three.Foreground = ScbBlue; 248 txt_Four.Foreground = ScbBlue; 249 txt_Five.Foreground = ScbBlue; 250 txt_Six.Foreground = ScbBlue; 251 switch (GOTO) 252 { 253 case 1: 254 goto GT1; 255 case 2: 256 goto GT2; 257 case 3: 258 goto GT3; 259 case 4: 260 goto GT4; 261 case 5: 262 goto GT5; 263 case 6: 264 goto GT6; 265 } 266 GT1: txt_One.Foreground = ScbRed; 267 return; 268 GT2: txt_Two.Foreground = ScbRed; 269 return; 270 GT3: txt_Three.Foreground = ScbRed; 271 return; 272 GT4: txt_Four.Foreground = ScbRed; 273 return; 274 GT5: txt_Five.Foreground = ScbRed; 275 return; 276 GT6: txt_Six.Foreground = ScbRed; 277 } 278 279 /// <summary> 280 /// 前四个数字变化 281 /// </summary> 282 /// <param name="InitialNumber">开始数字</param> 283 public void ForeFiveNumberChanged(int InitialNumber) 284 { 285 txt_One.Text = InitialNumber.ToString(); 286 txt_Two.Text = (InitialNumber + 1).ToString(); 287 txt_Three.Text = (InitialNumber + 2).ToString(); 288 txt_Four.Text = (InitialNumber + 3).ToString(); 289 } 290 291 /// <summary> 292 /// 设置后两位数字 293 /// </summary> 294 public void LatterTwo() 295 { 296 txt_Six.Text = PagerCount.ToString(); 297 if (PagerCount > 6) 298 txt_Five.Text = "…"; 299 else 300 txt_Five.Text = (PagerCount - 1).ToString(); 301 } 302 303 /// <summary> 304 /// 数字从尾数开始变化 305 /// </summary> 306 /// <param name="Mantissa">尾数</param> 307 public void LatterNumberChanged(int Mantissa) 308 { 309 txt_Six.Text = Mantissa.ToString(); 310 txt_Five.Text = (Mantissa - 1).ToString(); 311 txt_Four.Text = (Mantissa - 2).ToString(); 312 txt_Three.Text = (Mantissa - 3).ToString(); 313 txt_Two.Text = (Mantissa - 4).ToString(); 314 txt_One.Text = (Mantissa - 5).ToString(); 315 } 316 317 /// <summary> 318 /// 设置总页数 319 /// </summary> 320 public void SetPagerCount() 321 { 322 int pc = PagerTotal / PagerSize; 323 if (PagerTotal % PagerSize == 0) 324 PagerCount = pc; 325 else 326 PagerCount = pc + 1; 327 if (PagerCount <= 6) 328 CollapsedTXT(PagerCount); 329 txt_Total.Text = PagerTotal.ToString(); 330 } 331 332 /// <summary> 333 /// 小于6页的隐藏部分控件 334 /// </summary> 335 /// <param name="CollapsedStartTXT">从第几个开始</param> 336 public void CollapsedTXT(int CollapsedStartTXT) 337 { 338 switch (CollapsedStartTXT) 339 { 340 case 1: 341 goto CST1; 342 case 2: 343 goto CST2; 344 case 3: 345 goto CST3; 346 case 4: 347 goto CST4; 348 case 5: 349 goto CST5; 350 } 351 return; 352 CST1: txt_Two.Visibility = Visibility.Collapsed; 353 CST2: txt_Three.Visibility = Visibility.Collapsed; 354 CST3: txt_Four.Visibility = Visibility.Collapsed; 355 CST4: txt_Five.Visibility = Visibility.Collapsed; 356 CST5: txt_Six.Visibility = Visibility.Collapsed; 357 } 358 359 /// <summary> 360 /// 刷新 361 /// </summary> 362 public void Refresh() 363 { 364 SetPagerCount(); 365 ForeFiveNumberChanged(PagerIndex); 366 ColorChanged(PagerIndex); 367 LatterTwo(); 368 } 369 #endregion 370 }
二:定义资源字典文件,命名为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>
五:现在就可以用了,用法:
先引用:
再添加:
六:后台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(); }