新建Custom Control,名:PagingDataGrid
打开工程下面的Themes\Generic.xaml
xaml里面代码替换如下
1 <Style x:Key="{x:Type loc:PagingDataGrid}" TargetType="{x:Type loc:PagingDataGrid}"> 2 <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/> 3 <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 4 <Setter Property="BorderBrush" Value="#FF688CAF" /> 5 <Setter Property="BorderThickness" Value="1" /> 6 <Setter Property="RowDetailsVisibilityMode" Value="VisibleWhenSelected" /> 7 <Setter Property="ScrollViewer.CanContentScroll" Value="true"/> 8 <Setter Property="CanUserSortColumns" Value="False"/> 9 <Setter Property="AlternatingRowBackground" Value="SkyBlue"/> 10 <Setter Property="AlternationCount" Value="2"/> 11 <Setter Property="ScrollViewer.PanningMode" Value="Both"/> 12 <Setter Property="Stylus.IsFlicksEnabled" Value="False"/> 13 <Setter Property="Template"> 14 <Setter.Value> 15 <ControlTemplate TargetType="{x:Type loc:PagingDataGrid}"> 16 <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="True" Padding="{TemplateBinding Padding}"> 17 <Grid> 18 <Grid.RowDefinitions> 19 <RowDefinition Height="*"/> 20 <RowDefinition Height="Auto"/> 21 </Grid.RowDefinitions> 22 <ScrollViewer Grid.Row="0" Focusable="false" Name="DG_ScrollViewer"> 23 <ScrollViewer.Template> 24 <ControlTemplate TargetType="{x:Type ScrollViewer}"> 25 <Grid> 26 <Grid.ColumnDefinitions> 27 <ColumnDefinition Width="Auto"/> 28 <ColumnDefinition Width="*"/> 29 <ColumnDefinition Width="Auto"/> 30 </Grid.ColumnDefinitions> 31 <Grid.RowDefinitions> 32 <RowDefinition Height="Auto"/> 33 <RowDefinition Height="*"/> 34 <RowDefinition Height="Auto"/> 35 </Grid.RowDefinitions> 36 <Button Command="ApplicationCommands.SelectAll" Focusable="False" Style="{DynamicResource {ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}}" Width="{Binding CellsPanelHorizontalOffset, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type DataGrid}}}"> 37 <Button.Visibility> 38 <Binding Path="HeadersVisibility" RelativeSource="{RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type DataGrid}}"> 39 <Binding.ConverterParameter> 40 <DataGridHeadersVisibility>All</DataGridHeadersVisibility> 41 </Binding.ConverterParameter> 42 </Binding> 43 </Button.Visibility> 44 </Button> 45 <DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter" Grid.Column="1"> 46 <DataGridColumnHeadersPresenter.Visibility> 47 <Binding Path="HeadersVisibility" RelativeSource="{RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type DataGrid}}"> 48 <Binding.ConverterParameter> 49 <DataGridHeadersVisibility>Column</DataGridHeadersVisibility> 50 </Binding.ConverterParameter> 51 </Binding> 52 </DataGridColumnHeadersPresenter.Visibility> 53 </DataGridColumnHeadersPresenter> 54 <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" CanContentScroll="{TemplateBinding CanContentScroll}" CanHorizontallyScroll="False" Grid.ColumnSpan="2" CanVerticallyScroll="False" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" Grid.Row="1"/> 55 <ScrollBar x:Name="PART_VerticalScrollBar" Grid.Column="2" Maximum="{TemplateBinding ScrollableHeight}" Orientation="Vertical" Grid.Row="1" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportHeight}"/> 56 <Grid Grid.Column="1" Grid.Row="2"> 57 <Grid.ColumnDefinitions> 58 <ColumnDefinition Width="{Binding NonFrozenColumnsViewportHorizontalOffset, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type DataGrid}}}"/> 59 <ColumnDefinition Width="*"/> 60 </Grid.ColumnDefinitions> 61 <ScrollBar x:Name="PART_HorizontalScrollBar" Grid.Column="1" Maximum="{TemplateBinding ScrollableWidth}" Orientation="Horizontal" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportWidth}"/> 62 </Grid> 63 </Grid> 64 </ControlTemplate> 65 </ScrollViewer.Template> 66 <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 67 </ScrollViewer> 68 <!--分页控件--> 69 <StackPanel Grid.Row="1" SnapsToDevicePixels="True" VerticalAlignment="Center" Visibility="{Binding IsShowPaging,RelativeSource={RelativeSource TemplatedParent},Converter={StaticResource BoolToVisibilityConverter}}" Orientation="Horizontal"> 70 <StackPanel.Resources> 71 <Style TargetType="TextBlock"> 72 <Setter Property="FontSize" Value="13"/> 73 <Setter Property="Margin" Value="3,0"/> 74 </Style> 75 <Style TargetType="TextBox"> 76 <Setter Property="FontSize" Value="13"/> 77 </Style> 78 <Style TargetType="ComboBox"> 79 <Setter Property="FontSize" Value="13"/> 80 </Style> 81 <Style TargetType="Button"> 82 <Setter Property="FontSize" Value="13"/> 83 </Style> 84 </StackPanel.Resources> 85 <TextBlock>总计</TextBlock> 86 <TextBlock Foreground="Red" Text="{Binding Total,RelativeSource={RelativeSource TemplatedParent},Mode=OneWay}" /> 87 <TextBlock>条记录 每页显示</TextBlock> 88 <ComboBox x:Name="PART_DispalyCount" BorderThickness="1" BorderBrush="Red" Foreground="Red" Grid.Column="0" FontSize="12" VerticalAlignment="Center" MinWidth="40" ItemsSource="{Binding PageSizeItemsSource,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}" SelectedItem="{Binding PageSize,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" /> 89 <TextBlock>条 总页数</TextBlock> 90 <TextBlock Margin="3" Foreground="Red" Text="{Binding PageCount,RelativeSource={RelativeSource TemplatedParent},Mode=OneWay}" /> 91 <Button x:Name="PART_PrePage" Content="上一页"/> 92 <TextBlock>当前页</TextBlock> 93 <TextBox Width="100" Foreground="Red" x:Name="PART_PageIndex" Text="{Binding PageIndex,RelativeSource={RelativeSource TemplatedParent},Mode=OneWay}" /> 94 <TextBlock>页</TextBlock> 95 <Button x:Name="PART_GoTo" Content="转到页"/> 96 <Button x:Name="PART_NextPage" Content="下一页"/> 97 </StackPanel> 98 </Grid> 99 </Border> 100 </ControlTemplate> 101 </Setter.Value> 102 </Setter> 103 <Style.Triggers> 104 <Trigger Property="IsGrouping" Value="true"> 105 <Setter Property="ScrollViewer.CanContentScroll" Value="false"/> 106 </Trigger> 107 </Style.Triggers> 108 </Style>