您的位置:首页 > 其它

WPF分页控件

2015-10-29 14:14 225 查看
前台界面:pageBtn.xaml

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
mc:Ignorable="d"
x:Class="BIMClient.pageBtn"
x:Name="UserControl"
d:DesignWidth="369.853" d:DesignHeight="31.837">
<UserControl.Resources>
<!--pageIconfont-->
<Style x:Key="pageIconfont" TargetType="{x:Type TextBlock}">
<Setter Property="TextElement.FontFamily" Value="pack://application:,,,/Font/#iconfont" />
<Setter Property="TextElement.FontSize" Value="12" />
</Style>

<Style x:Key="currentPage" TargetType="{x:Type TextBlock}">
<Setter Property="FontSize" Value="14"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Foreground" Value="#0bb591"/>
<Setter Property="Margin" Value="2,0"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
<Style x:Key="topComboBoxStyle" TargetType="{x:Type ComboBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBox}">
<Grid>
<!-- ToggleButton 已数据绑定到 ComboBox 本身以切换 IsDropDownOpen -->
<ToggleButton x:Name="ToggleButton" Grid.Column="2"
Template="{DynamicResource ComboBoxToggleButton}"
Focusable="false"
IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
ClickMode="Press"/>
<ContentPresenter x:Name="ContentSite" HorizontalAlignment="Left"
Margin="10,3,23,3"
VerticalAlignment="Center"
TextBlock.Foreground="#666"
Content="{TemplateBinding SelectionBoxItem}"
ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
IsHitTestVisible="true"/>

<TextBox Visibility="Hidden"
HorizontalAlignment="Left"
Margin="3,3,0,3"
x:Name="PART_EditableTextBox"
Style="{x:Null}"
VerticalAlignment="Center"
Focusable="True"
Background="Transparent"
IsReadOnly="{TemplateBinding IsReadOnly}"/>

<Popup x:Name="Popup" IsOpen="{TemplateBinding IsDropDownOpen}"
Placement="Top"
Focusable="False"
AllowsTransparency="True"
PopupAnimation="Slide">
<Grid x:Name="DropDown" MaxHeight="{TemplateBinding MaxDropDownHeight}"
MinWidth="{TemplateBinding ActualWidth}"
SnapsToDevicePixels="True">
<Border x:Name="DropDownBorder" Background="#fff"
BorderBrush="#0bb591"
BorderThickness="1"/>
<ScrollViewer Margin="4,6,4,6"

SnapsToDevicePixels="True"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto"
CanContentScroll="True">
<!-- StackPanel 用于显示子级,方法是将 IsItemsHost 设置为 True -->
<StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" Background="White"/>
</ScrollViewer>
</Grid>
</Popup>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

<SolidColorBrush x:Key="ButtonNormalBorder" Color="#FF707070"/>
<Style x:Key="pageBtnGrid" TargetType="{x:Type Button}">
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Foreground" Value="#666" />
<Setter Property="Margin" Value="1,0,0,0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border  x:Name="grid" BorderBrush="Transparent"
BorderThickness="1"
Background="Transparent"
SnapsToDevicePixels="True"
Padding="10,7"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Cursor="Hand">
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" TextBlock.FontSize="12"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="#fff" />
<Setter Property="Background" TargetName="grid" Value="#0bb591" />
<Setter Property="BorderBrush" TargetName="grid" Value="#0bb591"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Foreground" Value="#fff" />
<Setter Property="Padding" TargetName="grid" Value="11,8,9,6"/>
<Setter Property="Background" TargetName="grid" Value="#039e7d"/>
<Setter Property="BorderBrush" TargetName="grid" Value="#039e7d"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="#aaa" />
</Trigger>
<Trigger Property="IsFocused" Value="true">
<Setter Property="Foreground" Value="#fff" />
<Setter Property="Background" TargetName="grid" Value="#0bb591" />
<Setter Property="BorderBrush" TargetName="grid" Value="#0bb591"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

</UserControl.Resources>

<Grid>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,10">
<StackPanel.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="FontSize" Value="12"/>
<Setter Property="Foreground" Value="#666"/>
<Setter Property="Margin" Value="10,0,5,0"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
</StackPanel.Resources>
<TextBlock Text="当前第" />
<TextBlock  Style="{DynamicResource currentPage}"  x:Name="TxtCurrentPage"/>
<TextBlock Text="页" />
<TextBlock Text="转到 " />
<ComboBox Margin="0,0,10,0"  Style="{DynamicResource topComboBoxStyle}"     x:Name="ComboxPage"  >

</ComboBox>
<Button IsEnabled="False" Style="{DynamicResource pageBtnGrid}" x:Name="BtnFirst"  Click="BtnFirst_Click" >
<TextBlock Text="" Style="{DynamicResource pageIconfont}"/>
</Button>
<Button Style="{DynamicResource pageBtnGrid}"  x:Name="BtnUp" Click="BtnUp_Click">
<TextBlock Text="" Style="{DynamicResource pageIconfont}"/>
</Button>
<StackPanel Orientation="Horizontal" x:Name="btnlist">

</StackPanel>
<Button IsEnabled="False" Style="{DynamicResource pageBtnGrid}" x:Name="BtnNext" Click="BtnNext_Click">
<TextBlock Text="" Style="{DynamicResource pageIconfont}"/>
</Button>
<Button Style="{DynamicResource pageBtnGrid}" x:Name="BtnLast" Click="BtnLast_Click">
<TextBlock Text="" Style="{DynamicResource pageIconfont}"/>
</Button>
</StackPanel>
</Grid>
</UserControl>


后台代码:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BIM.usecontrol;
using System.Collections;
using BIM.Backend.Modules;

namespace BIMClient
{
/// <summary>
/// pageBtn.xaml 的交互逻辑
/// </summary>
public partial class pageBtn : UserControl
{
/// <summary>
/// 总页数
/// </summary>
public int PageCount { get; set; }

/// <summary>
/// 当前页数
/// </summary>
public int CurrentPage { get; set; }

/// <summary>
/// 数据集控件
/// </summary>
public DataGrid DataGridControl { get; set; }

public string TableName{  get;set; }

public string FieldName
{
get;
set;
}
public string OtherCondition
{
get;
set;
}
public string OrderBy
{
get;
set;
}

public int PageSize
{
get;
set;
}

public string DataGridName
{
set;
get;
}

public string PagerName
{
set;
get;
}
public pageBtn()
{

this.InitializeComponent();
//绑定下拉框
// BindComBox();

TxtCurrentPage.Text = "1";
}

#region 按页数生成按钮
/// <summary>
/// 生成按钮
/// </summary>
public void GenerationBtn()
{
if (PageCount > 0)
{
btnlist.Children.Clear();
Style btn_style = (Style)this.FindResource("pageBtnGrid");
for (int i = 0; i < PageCount; i++)
{

System.Windows.Controls.Button PointBtn = new Button();
PointBtn.Content = (i + 1);
PointBtn.Name = "Btn" + (i + 1).ToString();
PointBtn.Style = btn_style;
PointBtn.Click += new RoutedEventHandler(PointPage_Click);

if (i + 1 > 10)
{
PointBtn.Visibility = System.Windows.Visibility.Collapsed;

}
btnlist.Children.Add(PointBtn);
}
}
}
#endregion

/// <summary>
/// 绑定下拉
/// </summary>
public class PageList
{
public int Page { get; set; }
}

#region ComBox绑定页数
public void BindComBox()
{
if (PageCount > 0)
{
List<PageList> pageList = new List<PageList>();
for (int i = 0; i < PageCount; i++)
{
PageList MyPage = new PageList();
MyPage.Page = (i + 1);
pageList.Add(MyPage);
}
ComboxPage.ItemsSource = pageList;
ComboxPage.DisplayMemberPath = "Page";
ComboxPage.SelectedValuePath = "Page";
TxtCurrentPage.Text = CurrentPage.ToString();
ComboxPage.SelectedValue = TxtCurrentPage.Text;
ComboxPage.SelectionChanged += ComboxPage_SelectionChanged;
}
}
#endregion
public void Refresh()
{
TxtCurrentPage.Text = "1";
// BindComBox();
GenerationBtn();
BtnEnabled();
}

#region 第一页
private void BtnFirst_Click(object sender, RoutedEventArgs e)
{
TxtCurrentPage.Text = "1";
// ComboxPage.SelectedValue = TxtCurrentPage.Text; 以前去掉
PointPage(Int32.Parse(TxtCurrentPage.Text));
BtnEnabled();
}
#endregion

#region 下一页
private void BtnNext_Click(object sender, RoutedEventArgs e)
{
TxtCurrentPage.Text = ((Convert.ToInt16(TxtCurrentPage.Text) - 1) == PageCount ? (PageCount + 1).ToString() : (Convert.ToInt16(TxtCurrentPage.Text) + 1).ToString());
PointPage(Int32.Parse(TxtCurrentPage.Text));  //提前来了
//ComboxPage.SelectedValue = TxtCurrentPage.Text;  以前
//GetBtn("Btn" + TxtCurrentPage.Text);     以前
BtnEnabled();
//PointPage(Int32.Parse(TxtCurrentPage.Text));  以前
}
#endregion

#region 上一页
private void BtnUp_Click(object sender, RoutedEventArgs e)
{
TxtCurrentPage.Text = ((Convert.ToInt16(TxtCurrentPage.Text) - 1) == 0 ? "1" : (Convert.ToInt16(TxtCurrentPage.Text) - 1).ToString());
PointPage(Int32.Parse(TxtCurrentPage.Text));  //新增
// ComboxPage.SelectedValue = TxtCurrentPage.Text; 以前去掉
BtnEnabled();
}
#endregion

#region 最后一页
private void BtnLast_Click(object sender, RoutedEventArgs e)
{
TxtCurrentPage.Text = (PageCount).ToString();
//  ComboxPage.SelectedValue = TxtCurrentPage.Text;  以前去掉
PointPage(PageCount);
BtnEnabled();
}
#endregion

#region 跳转到指定页面
/// <summary>
/// 跳转到指定页面
/// </summary>
/// <param name="Page">页码</param>
public void PointPage(int Page)
{
GlobalRepertories.TransmissionEngine.SignalRPaginationExBindDataGrid("", TableName, PageSize, "*", OtherCondition, OrderBy, Int32.Parse(TxtCurrentPage.Text), DataGridName, PagerName);
}
#endregion

public void GetBtn(string BtnId)
{
for (int i = 0; i < btnlist.Children.Count; i++)
{
if (((Button)btnlist.Children[i]).Name == BtnId)
{

((Button)btnlist.Children[i]).ForceCursor = true;
}
}

}
public void ShowPageBtn()
{
ArrayList BtnList = new ArrayList();
if (Int32.Parse(TxtCurrentPage.Text) != 1 && Int32.Parse(TxtCurrentPage.Text) != PageCount)
{

int SumNum = 5;
for (int m = 4; m >= 0; m--)
{
if ((Int32.Parse(TxtCurrentPage.Text) - m) <= 0)
{

}
else
{
BtnList.Add(Int32.Parse(TxtCurrentPage.Text) - m);
}

}
if (BtnList.Count <= 4)
{
SumNum = 10 - BtnList.Count;
}
for (int m = 1; m <= SumNum; m++)
{
if ((Int32.Parse(TxtCurrentPage.Text) + m) > PageCount)
{
break;
}
else
{
BtnList.Add(Int32.Parse(TxtCurrentPage.Text) + m);
}
}
int a = BtnList.Count - 2;
int max = Int32.Parse(BtnList[BtnList.Count - 1].ToString());
int min = Int32.Parse(BtnList[0].ToString());

if (max == PageCount && PageCount > 10 && BtnList.Count < 10)
{
for (int i = 10 - BtnList.Count; i > 0; i--)
{
BtnList.Add(min - i);
}
}
bool IsShow = false;
for (int i = 0; i < btnlist.Children.Count; i++)
{
for (int m = 0; m < BtnList.Count; m++)
{
IsShow = false;
if (((Button)btnlist.Children[i]).Name == "Btn" + BtnList[m].ToString())
{
IsShow = true;
break;
}
}
if (IsShow == true)
{
((Button)btnlist.Children[i]).Visibility = System.Windows.Visibility.Visible;
}
else
{
((Button)btnlist.Children[i]).Visibility = System.Windows.Visibility.Collapsed;
}

}
}

}

private void PointPage_Click(object sender, RoutedEventArgs e)
{

Button Btn = (Button)sender;

TxtCurrentPage.Text = Btn.Content.ToString();

PointPage(Convert.ToInt32(TxtCurrentPage.Text));
BtnEnabled();

}

public void BtnEnabled()
{

ShowPageBtn();
if (TxtCurrentPage.Text == "1")
{
BtnFirst.IsEnabled = false;
BtnUp.IsEnabled = false;

BtnLast.IsEnabled = true;
BtnNext.IsEnabled = true;

}
if (TxtCurrentPage.Text == Convert.ToString(PageCount))
{
BtnFirst.IsEnabled = true;
BtnNext.IsEnabled = true;

BtnLast.IsEnabled = false;
BtnNext.IsEnabled = false;
}
if (TxtCurrentPage.Text != "1" && TxtCurrentPage.Text != Convert.ToString(PageCount))
{
BtnLast.IsEnabled = true;
BtnNext.IsEnabled = true;
BtnFirst.IsEnabled = true;
BtnUp.IsEnabled = true;

}

}

public string Refresh_Page(int Page_Count, int Current_Page, string Table_Name, string Field_Name, string Other_Condition, string Order_By, string DataGrid_Name, int Page_Size, string Pager_Name)
{

PageCount = Page_Count;
CurrentPage = Current_Page;
FieldName = Field_Name;
OtherCondition = Other_Condition;
OrderBy = Order_By;
TxtCurrentPage.Text = Current_Page.ToString();
TableName = Table_Name;
PageSize = Page_Size;
PagerName = Pager_Name;
DataGridName = DataGrid_Name;

IsSystemOperation = true;
BindComBox();
IsSystemOperation = false;
GenerationBtn();
BtnEnabled();
return "";
}

private bool IsSystemOperation = false;
private void ComboxPage_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (IsSystemOperation) return;

if (ComboxPage.SelectedValue == null)
{
ComboxPage.SelectedValue = 1;
}
TxtCurrentPage.Text = ComboxPage.SelectedValue.ToString();
PointPage(Convert.ToInt16(ComboxPage.SelectedValue));
BtnEnabled();

}
private void ComboxPage_SelectionChanged2(object sender, SelectionChangedEventArgs e)
{

}

}
}


其它前台页面引用

xmlns:local="clr-namespace:BIMClient"
<local:pageBtn x:Name="pagnav"/>


后台:

#region datagrid绑定
/// <summary>
/// datagrid绑定
/// </summary>
public void dataBind()
{
//用户唯一标识
//视图名称
//每页条数
//字段名
//控件容器
//排序条件
//当前页数
//数据集控件ID(DataGrid)
//分页控件ID
GlobalRepertories.TransmissionEngine.SignalRPaginationExBindDataGrid(__UserRowGuid, "SyS_User", PageNum.BackNum(), "*", BIM.Backend.Modules.SqlWhere.StrWhere(txtName, cbxJueSe, cbxState), "  order by RowID desc", 1, "dataGrid", "pagnav");

}

#endregion
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  分页 wpf 界面 控件