WPF 自定义分页控件一

一:右键添加新建项,选择新建自定义控件,命名为:KDataPager

  1   public class KDataPager : Control
  2     {
  3         static KDataPager()
  4         {
  5             DefaultStyleKeyProperty.OverrideMetadata(typeof(KDataPager), new FrameworkPropertyMetadata(typeof(KDataPager)));
  6         }
  7 
  8         #region 变量定义
  9         ComboBox PART_DispalyCount;
 10         Label PART_PrePage;
 11         Label PART_NextPage;
 12         TextBox PART_PageIndex;
 13 
 14         // 定义13个按钮
 15         #region Label&Border
 16         Label lab1;
 17         Label lab2;
 18         Label lab3;
 19         Label lab4;
 20         Label lab5;
 21         Label lab6;
 22         Label lab7;
 23         Label lab8;
 24         Label lab9;
 25         Label lab10;
 26         Label lab11;
 27         Label lab12;
 28         Label lab13;
 29 
 30         Border bor1;
 31         Border bor2;
 32         Border bor3;
 33         Border bor4;
 34         Border bor5;
 35         Border bor6;
 36         Border bor7;
 37         Border bor8;
 38         Border bor9;
 39         Border bor10;
 40         Border bor11;
 41         Border bor12;
 42         Border bor13;
 43         #endregion Label&Border_end
 44 
 45         #endregion 变量定义_end
 46 
 47         // 下拉框的 ItemsSource
 48         List<string> list = new List<string>();
 49         public List<string> PageSizeItemsSource
 50         {
 51             get { return list; }
 52             set { list = value; }
 53         }
 54 
 55         #region 依赖属性
 56 
 57         /// <summary>
 58         /// 页大小
 59         /// </summary>
 60         public int PageSize
 61         {
 62             get { return (int)GetValue(PageSizeProperty); }
 63             set { SetValue(PageSizeProperty, value); }
 64         }
 65 
 66         /// <summary>
 67         /// 当前页
 68         /// </summary>
 69         public int PageIndex
 70         {
 71             get { return (int)GetValue(PageIndexProperty); }
 72             set { SetValue(PageIndexProperty, value); }
 73         }
 74 
 75         /// <summary>
 76         /// 总计录数
 77         /// </summary>
 78         public int Total
 79         {
 80             get { return (int)GetValue(TotalProperty); }
 81             set { SetValue(TotalProperty, value); }
 82         }
 83 
 84         /// <summary>
 85         /// 是否显示页
 86         /// </summary>
 87         public bool IsShowPaging
 88         {
 89             get { return (bool)GetValue(IsShowPagingProperty); }
 90             set { SetValue(IsShowPagingProperty, value); }
 91         }
 92 
 93         /// <summary>
 94         /// 总页数
 95         /// </summary>
 96         public int PageCount
 97         {
 98             get { return (int)GetValue(PageCountProperty); }
 99             set { SetValue(PageCountProperty, value); }
100         }
101 
102         /// <summary>
103         /// 按钮数
104         /// </summary>
105         int ButtonCount = 13;
106 
107         /// <summary>
108         /// 数据源
109         /// </summary>
110 
111         //使用一个依赖属性作为PageCount的后备存储器。这支持动画、样式、绑定等。
112         public static readonly DependencyProperty PageCountProperty =
113             DependencyProperty.Register("PageCount", typeof(int), typeof(KDataPager), new UIPropertyMetadata(13));
114 
115 
116         //使用一个可靠的属性作为结束的后备存储器。这支持动画、样式、绑定等。
117         public static readonly DependencyProperty EndProperty =
118             DependencyProperty.Register("End", typeof(int), typeof(KDataPager), new UIPropertyMetadata(0));
119 
120 
121         //使用一个依赖属性作为开始的后备存储器。这支持动画、样式、绑定等。
122         public static readonly DependencyProperty StartProperty =
123             DependencyProperty.Register("Start", typeof(int), typeof(KDataPager), new UIPropertyMetadata(0));
124 
125 
126         //使用一个依赖属性作为isshow分页的后备存储器。这支持动画、样式、绑定等。
127         public static readonly DependencyProperty IsShowPagingProperty =
128             DependencyProperty.Register("IsShowPaging", typeof(bool), typeof(KDataPager), new UIPropertyMetadata(true));
129 
130 
131         //使用一个可靠的属性作为总的后备存储器。这支持动画、样式、绑定等
132         public static readonly DependencyProperty TotalProperty =
133             DependencyProperty.Register("Total", typeof(int), typeof(KDataPager), new UIPropertyMetadata(151));
134 
135 
136         //使用一个依赖属性作为PageIndex的后备存储器。这支持动画、样式、绑定等。
137         public static readonly DependencyProperty PageIndexProperty =
138             DependencyProperty.Register("PageIndex", typeof(int), typeof(KDataPager), new UIPropertyMetadata(1));
139 
140 
141         //使用一个依赖属性作为PageSize的后备存储器。这支持动画、样式、绑定等。
142         public static readonly DependencyProperty PageSizeProperty =
143             DependencyProperty.Register("PageSize", typeof(int), typeof(KDataPager), new UIPropertyMetadata(10));
144 
145         #endregion 依赖属性_end
146 
147         //相关命令
148         /// 下一页
149         RoutedCommand NextPage_Excuted = new RoutedCommand();
150         /// 上一页
151         RoutedCommand PrePageCommand = new RoutedCommand();
152         /// 跳转页
153         RoutedCommand GoToCommand = new RoutedCommand();
154 
155         /// <summary>
156         /// 重写方法
157         /// </summary>
158         public override void OnApplyTemplate()
159         {
160             base.OnApplyTemplate();
161 
162             #region 获取模板中的控件
163             PART_DispalyCount = GetTemplateChild("PART_DispalyCount") as ComboBox;
164             PART_PrePage = GetTemplateChild("PART_PrePage") as Label;
165             PART_NextPage = GetTemplateChild("PART_NextPage") as Label;
166             PART_PageIndex = GetTemplateChild("PART_PageIndex") as TextBox;
167 
168             #region label & border控件
169             //label
170             lab1 = GetTemplateChild("lab1") as Label;
171             lab2 = GetTemplateChild("lab2") as Label;
172             lab3 = GetTemplateChild("lab3") as Label;
173             lab4 = GetTemplateChild("lab4") as Label;
174             lab5 = GetTemplateChild("lab5") as Label;
175             lab6 = GetTemplateChild("lab6") as Label;
176             lab7 = GetTemplateChild("lab7") as Label;
177             lab8 = GetTemplateChild("lab8") as Label;
178             lab9 = GetTemplateChild("lab9") as Label;
179             lab10 = GetTemplateChild("lab10") as Label;
180             lab11 = GetTemplateChild("lab11") as Label;
181             lab12 = GetTemplateChild("lab12") as Label;
182             lab13 = GetTemplateChild("lab13") as Label;
183             //border
184             bor1 = GetTemplateChild("bor1") as Border;
185             bor2 = GetTemplateChild("bor2") as Border;
186             bor3 = GetTemplateChild("bor3") as Border;
187             bor4 = GetTemplateChild("bor4") as Border;
188             bor5 = GetTemplateChild("bor5") as Border;
189             bor6 = GetTemplateChild("bor6") as Border;
190             bor7 = GetTemplateChild("bor7") as Border;
191             bor8 = GetTemplateChild("bor8") as Border;
192             bor9 = GetTemplateChild("bor9") as Border;
193             bor10 = GetTemplateChild("bor10") as Border;
194             bor11 = GetTemplateChild("bor11") as Border;
195             bor12 = GetTemplateChild("bor12") as Border;
196             bor13 = GetTemplateChild("bor13") as Border;
197             #endregion label & border控件_end
198 
199             #endregion 获取模板中的控件_end
200 
201             //添加控件事件
202             PART_NextPage.MouseLeftButtonUp += PART_NextPage_MouseLeftButtonUp; ;
203             PART_PrePage.MouseLeftButtonUp += PART_PrePage_MouseLeftButtonUp; ;
204             PART_PageIndex.KeyUp += GOTO;
205             PART_DispalyCount.SelectionChanged += PART_DispalyCount_SelectionChanged;
206             //label 绑定同个事件
207             lab1.MouseLeftButtonUp += Lab1_MouseLeftButtonUp;
208             lab2.MouseLeftButtonUp += Lab1_MouseLeftButtonUp;
209             lab3.MouseLeftButtonUp += Lab1_MouseLeftButtonUp;
210             lab4.MouseLeftButtonUp += Lab1_MouseLeftButtonUp;
211             lab5.MouseLeftButtonUp += Lab1_MouseLeftButtonUp;
212             lab6.MouseLeftButtonUp += Lab1_MouseLeftButtonUp;
213             lab7.MouseLeftButtonUp += Lab1_MouseLeftButtonUp;
214             lab8.MouseLeftButtonUp += Lab1_MouseLeftButtonUp;
215             lab9.MouseLeftButtonUp += Lab1_MouseLeftButtonUp;
216             lab10.MouseLeftButtonUp += Lab1_MouseLeftButtonUp;
217             lab11.MouseLeftButtonUp += Lab1_MouseLeftButtonUp;
218             lab12.MouseLeftButtonUp += Lab1_MouseLeftButtonUp;
219             lab13.MouseLeftButtonUp += Lab1_MouseLeftButtonUp;
220             //加载
221             ReCalcLayout();
222         }
223 
224         #region 事件
225 
226         /// <summary>
227         /// 点击
228         /// </summary>
229         private void Lab1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
230         {
231             Label dg = sender as Label;
232             //获取点击按钮的值
233             var cont = dg.Content.ToString();
234             if (cont != "...")
235             {
236                 //清除其他按钮样式,设置点击按钮的样式
237                 BtnClick(cont);
238             }
239         }
240 
241         /// <summary>
242         /// 上一页
243         /// </summary>
244         private void PART_PrePage_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
245         {
246             if (PageIndex > 1)
247             {
248                 if (PageCount - PageIndex < (ButtonCount - 1))
249                 {
250                     BorderPressedStyle(ButtonCount - (PageCount - PageIndex) - 1);
251                     PageIndex--;
252                 }
253                 else
254                 {
255                     PageIndex--;
256                     BeforeSeven(PageIndex);
257                 }
258                 if (PageCount - PageIndex == ButtonCount)
259                     lab8.Content = "...";
260             }
261         }
262 
263         /// <summary>
264         /// 下一页
265         /// </summary>
266         private void PART_NextPage_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
267         {
268             if (PageCount - (ButtonCount - 1) > PageIndex)
269             {
270                 PageIndex++;
271                 if (PageCount - (ButtonCount - 1) == PageIndex)
272                     lab8.Content = PageCount - 5;
273                 BeforeSeven(PageIndex);
274             }
275             else
276             {
277                 if (PageIndex < PageCount)
278                 {
279                     PageIndex++;
280                     // ButtonCount - (PageCount - PageIndex); --
281                     BorderPressedStyle(ButtonCount - (PageCount - PageIndex));
282                 }
283             }
284         }
285 
286         /// <summary>
287         /// 跳转
288         /// </summary>
289         private void GOTO(object sender, KeyEventArgs e)
290         {
291             if (e.Key == Key.Enter)
292             {
293                 int page = 0;
294                 try
295                 {
296                     page = (int)Convert.ToDouble(PART_PageIndex.Text);
297                 }
298                 catch
299                 {
300                     MessageBox.Show("请输入正确的数值!");
301                 }
302                 if (page > PageCount)
303                 {
304                     PageIndex = PageCount;
305                 }
306                 else if (page < 1)
307                 {
308                     PageIndex = 1;
309                 }
310                 else
311                 {
312                     PageIndex = page;
313                     if (PageCount - PageIndex >= ButtonCount)
314                         lab8.Content = "...";
315                 }
316                 BtnClick(PageIndex.ToString());
317             }
318         }
319 
320         /// <summary>
321         /// 改变选中值发生的事件
322         /// </summary>
323         void PART_DispalyCount_SelectionChanged(object sender, SelectionChangedEventArgs e)
324         {
325             ReCalcLayout();
326         }
327 
328         #endregion 事件_end
329 
330         #region 方法
331 
332         /// <summary>
333         /// 点击按钮
334         /// </summary>
335         /// <param name="cont">点击按钮的数值</param>
336         public void BtnClick(string count)
337         {
338             var ct = int.Parse(count);
339             PageIndex = ct;
340             if (PageCount - PageIndex >= ButtonCount)
341             {
342                 BorderPressedStyle(1);
343                 BeforeSeven(ct);
344             }
345             else if (PageCount - PageIndex == (ButtonCount - 1))
346             {
347                 BorderPressedStyle(1);
348                 if (PageCount > ButtonCount)
349                     lab8.Content = PageCount - 5;
350                 BeforeSeven(PageIndex);
351             }
352             else
353             {
354                 BorderPressedStyle(ButtonCount - (PageCount - ct));
355                 if (PageCount > ButtonCount)
356                     lab8.Content = PageCount - 5;
357                 BeforeSeven(PageCount - (ButtonCount - 1));
358             }
359         }
360 
361         /// <summary>
362         /// 后6个label控件赋值
363         /// </summary>
364         public void AfterSix(int count)
365         {
366             Hidden(count + 1);
367             if (count <= 13)
368                 Assign(count);
369             else /* if (count > ButtonCount) */
370             {
371                 lab13.Content = count;
372                 lab12.Content = count - 1;
373                 lab11.Content = count - 2;
374                 lab10.Content = count - 3;
375                 lab9.Content = count - 4;
376                 if (count > 13)
377                 {
378                     lab8.Content = "...";
379                 }
380                 else
381                 {
382                     lab9.Content = count - 5;
383                 }
384             }
385         }
386 
387         /// <summary>
388         /// 前7个label控件赋值
389         /// </summary>
390         public void BeforeSeven(int count)
391         {
392             lab1.Content = count;
393             lab2.Content = count + 1;
394             lab3.Content = count + 2;
395             lab4.Content = count + 3;
396             lab5.Content = count + 4;
397             lab6.Content = count + 5;
398             lab7.Content = count + 6;
399         }
400 
401         /// <summary>
402         /// 小于ButtonCount页的直接显示
403         /// </summary>
404         /// <param name="Q"></param>
405         public void Assign(int LabelStart)
406         {
407             switch (LabelStart)
408             {
409                 case 1:
410                     goto q1;
411                 case 2:
412                     goto q2;
413                 case 3:
414                     goto q3;
415                 case 4:
416                     goto q4;
417                 case 5:
418                     goto q5;
419                 case 6:
420                     goto q6;
421                 case 7:
422                     goto q7;
423                 case 8:
424                     goto q8;
425                 case 9:
426                     goto q9;
427                 case 10:
428                     goto q10;
429                 case 11:
430                     goto q11;
431                 case 12:
432                     goto q12;
433                 case 13:
434                     goto q13;
435             }
436             return;
437             q13: lab13.Content = 13;
438             q12: lab12.Content = 12;
439             q11: lab11.Content = 11;
440             q10: lab10.Content = 10;
441             q9: lab9.Content = 9;
442             q8: lab8.Content = 8;
443             q7: lab7.Content = 7;
444             q6: lab6.Content = 6;
445             q5: lab5.Content = 5;
446             q4: lab4.Content = 4;
447             q3: lab3.Content = 3;
448             q2: lab2.Content = 2;
449             q1: lab1.Content = 1;
450         }
451 
452         /// <summary>
453         /// 小于ButtonCount的隐藏部分按钮
454         /// </summary>
455         /// <param name="P"></param>
456         public void Hidden(int BorderStart)
457         {
458             //全部显示
459             bor1.Visibility = Visibility.Visible;
460             bor2.Visibility = Visibility.Visible;
461             bor3.Visibility = Visibility.Visible;
462             bor4.Visibility = Visibility.Visible;
463             bor5.Visibility = Visibility.Visible;
464             bor6.Visibility = Visibility.Visible;
465             bor7.Visibility = Visibility.Visible;
466             bor8.Visibility = Visibility.Visible;
467             bor9.Visibility = Visibility.Visible;
468             bor10.Visibility = Visibility.Visible;
469             bor11.Visibility = Visibility.Visible;
470             bor12.Visibility = Visibility.Visible;
471             bor13.Visibility = Visibility.Visible;
472             switch (BorderStart)
473             {
474                 case 1:
475                     goto p1;
476                 case 2:
477                     goto p2;
478                 case 3:
479                     goto p3;
480                 case 4:
481                     goto p4;
482                 case 5:
483                     goto p5;
484                 case 6:
485                     goto p6;
486                 case 7:
487                     goto p7;
488                 case 8:
489                     goto p8;
490                 case 9:
491                     goto p9;
492                 case 10:
493                     goto p10;
494                 case 11:
495                     goto p11;
496                 case 12:
497                     goto p12;
498                 case 13:
499                     goto p13;
500             }
501             return;
502             // 之后部分隐藏
503             p1: bor1.Visibility = Visibility.Hidden;
504             p2: bor2.Visibility = Visibility.Hidden;
505             p3: bor3.Visibility = Visibility.Hidden;
506             p4: bor4.Visibility = Visibility.Hidden;
507             p5: bor5.Visibility = Visibility.Hidden;
508             p6: bor6.Visibility = Visibility.Hidden;
509             p7: bor7.Visibility = Visibility.Hidden;
510             p8: bor8.Visibility = Visibility.Hidden;
511             p9: bor9.Visibility = Visibility.Hidden;
512             p10: bor10.Visibility = Visibility.Hidden;
513             p11: bor11.Visibility = Visibility.Hidden;
514             p12: bor12.Visibility = Visibility.Hidden;
515             p13: bor13.Visibility = Visibility.Hidden;
516         }
517 
518         /// <summary>
519         /// 普通的样式
520         /// </summary>
521         public void UnPressedStyle(Border b, Label l)
522         {
523             b.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFFFFF"));
524             b.BorderThickness = new Thickness(1);
525             b.BorderBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#D9D9D9"));
526             l.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#000000"));
527         }
528 
529         /// <summary>
530         /// 按下的样式
531         /// </summary>
532         public void PressedStyle(Border b, Label l)
533         {
534             b.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#E9F5FF"));
535             b.BorderThickness = new Thickness(0);
536             l.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#3A5F8A"));
537         }
538 
539         /// <summary>
540         /// 哪个border是按下的样式
541         /// </summary>
542         /// <param name="O">控件值</param>
543         public void BorderPressedStyle(int WhichOneBorder)
544         {
545             //全部设置为普通样式
546             UnPressedStyle(bor1, lab1);
547             UnPressedStyle(bor2, lab2);
548             UnPressedStyle(bor3, lab3);
549             UnPressedStyle(bor4, lab4);
550             UnPressedStyle(bor5, lab5);
551             UnPressedStyle(bor6, lab6);
552             UnPressedStyle(bor7, lab7);
553             UnPressedStyle(bor8, lab8);
554             UnPressedStyle(bor9, lab9);
555             UnPressedStyle(bor10, lab10);
556             UnPressedStyle(bor11, lab11);
557             UnPressedStyle(bor12, lab12);
558             UnPressedStyle(bor13, lab13);
559             //按下的设置为按下样式
560             switch (WhichOneBorder)
561             {
562                 case 1:
563                     PressedStyle(bor1, lab1); break;
564                 case 2:
565                     PressedStyle(bor2, lab2); break;
566                 case 3:
567                     PressedStyle(bor3, lab3); break;
568                 case 4:
569                     PressedStyle(bor4, lab4); break;
570                 case 5:
571                     PressedStyle(bor5, lab5); break;
572                 case 6:
573                     PressedStyle(bor6, lab6); break;
574                 case 7:
575                     PressedStyle(bor7, lab7); break;
576                 case 8:
577                     PressedStyle(bor8, lab8); break;
578                 case 9:
579                     PressedStyle(bor9, lab9); break;
580                 case 10:
581                     PressedStyle(bor10, lab10); break;
582                 case 11:
583                     PressedStyle(bor11, lab11); break;
584                 case 12:
585                     PressedStyle(bor12, lab12); break;
586                 case 13:
587                     PressedStyle(bor13, lab13); break;
588             }
589         }
590 
591         /// <summary>
592         /// 获取ComboBox显示的页记录数
593         /// </summary>
594         public void GetPageSize(ComboBox cbbx)
595         {
596             string str = cbbx.SelectedItem.ToString();
597             str = Regex.Match(str, ".+\d").ToString();
598             PageSize = int.Parse(str.Substring(str.IndexOf(":") + 1, str.Length));
599         }
600 
601         /// <summary>
602         /// 刷新页数
603         /// </summary>
604         private void ReCalcLayout()
605         {
606             GetPageSize(PART_DispalyCount);
607             int pc = Total / PageSize;
608             if (Total % PageSize == 0)
609             {
610                 PageCount = pc;
611             }
612             else
613             {
614                 PageCount = pc + 1;
615             }
616             //刷新后从第一页开始
617             PageIndex = 1;
618             BorderPressedStyle(PageIndex);
619             BeforeSeven(1);
620             AfterSix(PageCount);
621             ButtonCount = PageCount > 13 ? 13 : PageCount;
622         }
623         #endregion 方法_end
624 
625     }
View Code

二:删除Themes/Generic.xaml里面新添加的模板:

//没错了,就是它,删除掉吧
<Style TargetType="{x:Type local:KDataPager}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:KDataPager}"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style>

三:在Themes中添加资源字典文件,命名为DataPager.xaml,并换成如下代码:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:loc="clr-namespace:Kingo.ProvinceLandSurvey.BaseControl.DataPager"
                    xmlns:my="clr-namespace:Kingo.ProvinceLandSurvey.BaseControl"
                    >
    <Style x:Key="borL" TargetType="Border">
        <Setter Property="BorderThickness" Value="1"></Setter>
        <Setter Property="BorderBrush" Value="#D9D9D9"></Setter>
        <Setter Property="CornerRadius" Value="2"></Setter>
        <Setter Property="Width" Value="28"></Setter>
        <Setter Property="Height" Value="28"></Setter>
        <Setter Property="Margin" Value="4"></Setter>
        <Setter Property="Cursor" Value="Hand"></Setter>
    </Style>
    <Style x:Key="labL" TargetType="Label">
        <Setter Property="Padding" Value="2,0,2,0"></Setter>
        <Setter Property="VerticalContentAlignment" Value="Center"></Setter>
        <Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
    </Style>
    <Style x:Key="LabDowe" TargetType="Label">
        <Setter Property="FontSize" Value="12"></Setter>
        <Setter Property="Foreground" Value="#999999"></Setter>
        <Setter Property="VerticalContentAlignment" Value="Center"></Setter>
        <Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="#F2F2F2"></Setter>
            </Trigger>
            <Trigger Property="IsMouseOver" Value="False">
                <Setter Property="Background" Value="#FFF"></Setter>
            </Trigger>
        </Style.Triggers>
    </Style>
    <Style x:Key="{x:Type loc:KDataPager}" TargetType="{x:Type loc:KDataPager}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type loc:KDataPager}">
                    <Border Background="#FFF">
                        <StackPanel Orientation="Horizontal">
                            <!--#region 上一页-->
                            <Border BorderThickness="1" BorderBrush="#D9D9D9" CornerRadius="2" Width="28" Height="28" Margin="2">
                                <Label x:Name="PART_PrePage" Content="&lt;" Style="{StaticResource labL}"></Label>
                            </Border>

                            <!--#region 13页-->
                            <Border>
                                <StackPanel Orientation="Horizontal">
                                    <Border x:Name="bor1" BorderThickness="0" Background="#E9F5FF" Style="{StaticResource borL}">
                                        <Label  x:Name="lab1" Content="1" Foreground="#3A5F8A" Style="{StaticResource labL}"></Label>
                                    </Border>
                                    <Border x:Name="bor2" Style="{StaticResource borL}">
                                        <Label  x:Name="lab2" Content="2" Style="{StaticResource labL}"></Label>
                                    </Border>
                                    <Border x:Name="bor3" Style="{StaticResource borL}">
                                        <Label  x:Name="lab3" Content="3" Style="{StaticResource labL}"></Label>
                                    </Border>
                                    <Border x:Name="bor4" Style="{StaticResource borL}">
                                        <Label  x:Name="lab4" Content="4" Style="{StaticResource labL}"></Label>
                                    </Border>
                                    <Border x:Name="bor5" Style="{StaticResource borL}">
                                        <Label  x:Name="lab5" Content="5" Style="{StaticResource labL}"></Label>
                                    </Border>
                                    <Border x:Name="bor6" Style="{StaticResource borL}">
                                        <Label   x:Name="lab6" Content="6" Style="{StaticResource labL}"></Label>
                                    </Border>
                                    <Border x:Name="bor7" Style="{StaticResource borL}">
                                        <Label  x:Name="lab7" Content="7" Style="{StaticResource labL}"></Label>
                                    </Border>
                                    <Border x:Name="bor8" Style="{StaticResource borL}">
                                        <Label  x:Name="lab8" Content="8" Style="{StaticResource labL}"></Label>
                                    </Border>
                                    <Border x:Name="bor9" Style="{StaticResource borL}">
                                        <Label  x:Name="lab9" Content="9" Style="{StaticResource labL}"></Label>
                                    </Border>
                                    <Border x:Name="bor10" Style="{StaticResource borL}">
                                        <Label  x:Name="lab10" Content="10" Style="{StaticResource labL}"></Label>
                                    </Border>
                                    <Border x:Name="bor11" Style="{StaticResource borL}">
                                        <Label  x:Name="lab11" Content="11" Style="{StaticResource labL}"></Label>
                                    </Border>
                                    <Border x:Name="bor12" Style="{StaticResource borL}">
                                        <Label  x:Name="lab12" Content="12" Style="{StaticResource labL}"></Label>
                                    </Border>
                                    <Border x:Name="bor13" Style="{StaticResource borL}">
                                        <Label  x:Name="lab13" Content="13" Style="{StaticResource labL}"></Label>
                                    </Border>
                                </StackPanel>
                            </Border>
                            <!--#endregion 13页_end-->

                            <!--#region 下一页-->
                            <Border BorderThickness="1" BorderBrush="#D9D9D9" CornerRadius="2" Width="28" Height="28" Margin="2">
                                <Label x:Name="PART_NextPage" Content="&gt;" Style="{StaticResource labL}"></Label>
                            </Border>

                            <!--#region 条数-->
                            <Border Margin="30,-40,0,0">
                                <StackPanel Orientation="Horizontal">
                                    <Border BorderBrush="#D9D9D9" CornerRadius="2,0,0,2" Height="28" Margin="0,40,0,0">
                                        <Border.BorderThickness>
                                            <Thickness Top="1" Bottom="1" Left="1"></Thickness>
                                        </Border.BorderThickness>
                                        <my:KComboBox x:Name="PART_DispalyCount" Width="100" Height="30" Foreground="#666" FontFamily="'kern' 1" FontSize="14" Padding="5,0,0,0" SelectedIndex="0"
                                                       ItemsSource="{Binding PageSizeItemsSource,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}">
                                        </my:KComboBox>
                                    </Border>
                                </StackPanel>
                            </Border>

                            <!--#region 跳转-->
                            <Border Margin="8,0,0,0">
                                <StackPanel Orientation="Horizontal">
                                    <Label Content="跳至" Style="{StaticResource labL}" Margin="5"></Label>
                                    <Border Width="47" Height="28" CornerRadius="2" BorderThickness="1" BorderBrush="#D9D9D9">
                                        <TextBox x:Name="PART_PageIndex" Height="22" VerticalContentAlignment="Center" HorizontalContentAlignment="Left" BorderThickness="0"></TextBox>
                                    </Border>
                                    <Label Content="页" Style="{StaticResource labL}" Margin="5"></Label>
                                </StackPanel>
                            </Border>

                        </StackPanel>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>
View Code

四:在Themes/Generic.xaml里面加上上一步添加的资源字典文件

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

五:用法:

               //头文件添加引用:xmlns:sys="clr-namespace:System;assembly=mscorlib"
         <my:KDataPager Total="300"> <my:KDataPager.PageSizeItemsSource> <sys:String>10条/页</sys:String> <sys:String>20条/页</sys:String> <sys:String>30条/页</sys:String> <sys:String>50条/页</sys:String> <sys:String>100条/页</sys:String> <sys:String>200条/页</sys:String> </my:KDataPager.PageSizeItemsSource> </my:KDataPager>

六:说明:

资源字典中嵌套了一个自定义控件,继承的是ComboBox,名称是KComboBox。如下:

<my:KComboBox x:Name="PART_DispalyCount" Width="100" Height="30" Foreground="#666" FontFamily="'kern' 1" FontSize="14" Padding="5,0,0,0" SelectedIndex="0"
                                                       ItemsSource="{Binding PageSizeItemsSource,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}">
                                        </my:KComboBox>

换成下面的代码,照样可以

 <ComboBox x:Name="PART_DispalyCount" Width="100" Height="30" Foreground="#666" FontFamily="'kern' 1" FontSize="14" Padding="5,0,0,0" SelectedIndex="0"
                                                       ItemsSource="{Binding PageSizeItemsSource,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}">
                                        </ComboBox>
原文地址:https://www.cnblogs.com/SeNaiTes/p/9633500.html