Silverlight学习笔记七之Silverlight的分页与表单(DataPager,DataForm控件)
这一节学习DataPager,DataForm控件
效果如图
、
DataPager
DataForm
1.DataPagerDemo.xaml
<UserControl x:Class="Silverlight.Common.View.DataPagerDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
xmlns:input="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit">
<Grid Background="White">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="200" />
<RowDefinition Height="200" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200" MinWidth="200" />
<ColumnDefinition Width="500" />
</Grid.ColumnDefinitions>
<ContentControl Content="显示样式:" />
<ContentControl Content="数据" Grid.Column="1" />
<data:DataGrid Grid.Column="1" IsReadOnly="False" Grid.Row="1" ItemsSource="{Binding}" Margin="5" />
<ListBox ItemsSource="{Binding}" Grid.Row="2" Grid.Column="1" Margin="5">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"/>
<TextBlock Text=" - " />
<TextBlock Text="{Binding CodeFaa}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<StackPanel Grid.Row="1" Grid.RowSpan="2">
<TextBlock Text="FirstLastNumeric样式:" />
<data:DataPager x:Name="dataPager" DisplayMode="FirstLastNumeric" Margin="0,0,0,4" HorizontalAlignment="Center" Source="{Binding}" NumericButtonCount="{Binding Value, ElementName=numericButtonCount}" AutoEllipsis="{Binding IsChecked, ElementName=autoEllipsis}" IsTotalItemCountFixed="{Binding IsChecked, ElementName=totalItemCountFixed}" />
<TextBlock Text="FirstLastPreviousNext样式:" />
<data:DataPager DisplayMode="FirstLastPreviousNext" Margin="0,0,0,4" HorizontalAlignment="Center" Source="{Binding}" NumericButtonCount="{Binding Value, ElementName=numericButtonCount}" AutoEllipsis="{Binding IsChecked, ElementName=autoEllipsis}" IsTotalItemCountFixed="{Binding IsChecked, ElementName=totalItemCountFixed}" />
<TextBlock Text="FirstLastPreviousNextNumeric样式:" />
<data:DataPager DisplayMode="FirstLastPreviousNextNumeric" Margin="0,0,0,4" HorizontalAlignment="Center" Source="{Binding}" NumericButtonCount="{Binding Value, ElementName=numericButtonCount}" AutoEllipsis="{Binding IsChecked, ElementName=autoEllipsis}" IsTotalItemCountFixed="{Binding IsChecked, ElementName=totalItemCountFixed}" />
<TextBlock Text="Numeric样式:" />
<data:DataPager DisplayMode="Numeric" Margin="0,0,0,4" HorizontalAlignment="Center" Source="{Binding}" NumericButtonCount="{Binding Value, ElementName=numericButtonCount}" AutoEllipsis="{Binding IsChecked, ElementName=autoEllipsis}" IsTotalItemCountFixed="{Binding IsChecked, ElementName=totalItemCountFixed}" />
<TextBlock Text="PreviousNext样式:" />
<data:DataPager DisplayMode="PreviousNext" Margin="0,0,0,4" HorizontalAlignment="Center" Source="{Binding}" NumericButtonCount="{Binding Value, ElementName=numericButtonCount}" AutoEllipsis="{Binding IsChecked, ElementName=autoEllipsis}" IsTotalItemCountFixed="{Binding IsChecked, ElementName=totalItemCountFixed}" />
<TextBlock Text="PreviousNextNumeric样式:" />
<data:DataPager DisplayMode="PreviousNextNumeric" Margin="0,0,0,4" HorizontalAlignment="Center" Source="{Binding}" NumericButtonCount="{Binding Value, ElementName=numericButtonCount}" AutoEllipsis="{Binding IsChecked, ElementName=autoEllipsis}" IsTotalItemCountFixed="{Binding IsChecked, ElementName=totalItemCountFixed}" />
<CheckBox x:Name="autoEllipsis" Content="AutoEllipsis" Margin="4" />
<CheckBox x:Name="totalItemCountFixed" Content="IsTotalItemCountFixed属性" IsChecked="True" Margin="4" />
<input:NumericUpDown x:Name="numpageSize" ValueChanging="numpageSize_ValueChanging"></input:NumericUpDown>
<Grid Height="0">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Text="NumericButtonCount:" VerticalAlignment="Center" Grid.Row="0" Grid.Column="0" Margin="4" />
<input:NumericUpDown x:Name="numericButtonCount" Value="5" Minimum="0" Maximum="20" VerticalAlignment="Center" Grid.Row="0" Grid.Column="1" Margin="4" />
<TextBlock Text="PageSize:" VerticalAlignment="Center" Grid.Row="1" Grid.Column="0" Margin="4" />
<input:NumericUpDown Value="{Binding PageSize, Mode=TwoWay}" Minimum="1" VerticalAlignment="Center" Grid.Row="1" Grid.Column="1" Margin="4" />
</Grid>
</StackPanel>
</Grid>
</Grid>
</UserControl>
2.DataPagerDemo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using Silverlight.Common.Core;
namespace Silverlight.Common.View
{
public partial class DataPagerDemo : UserControl
{
public DataPagerDemo()
{
InitializeComponent();
if (UserList.GetUserList().Count > 0)
{
PagedCollectionView pcv = new PagedCollectionView(UserList.GetUserList());
pcv.PageSize = 2;
DataContext = pcv;
}
else {
MessageBox.Show("没有数据");
}
}
private void numpageSize_ValueChanging(object sender, RoutedPropertyChangingEventArgs<double> e)
{
if (this.numpageSize.Value>0)
{
this.dataPager.PageSize =Convert.ToInt32(this.numpageSize.Value);
}
}
}
}
3.DataFormDemo.xaml
<UserControl x:Class="Silverlight.Common.View.DataFormDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dataform="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.DataForm.Toolkit"
xmlns:input="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<StackPanel>
<ContentControl Content="DataForm" />
<StackPanel>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0">
<CheckBox Content="AutoCommit" IsChecked="{Binding AutoCommit, ElementName=dataForm, Mode=TwoWay}" Margin="4" />
<CheckBox Content="AutoEdit" IsChecked="{Binding AutoEdit, ElementName=dataForm, Mode=TwoWay}" Margin="4" />
<TextBlock Text="CommandButtonsVisibility" Margin="4" />
<ComboBox SelectedItem="{Binding CommandButtonsVisibility, ElementName=dataForm, Mode=TwoWay}" SelectedIndex="0" Margin="4">
<sys:String>All</sys:String>
<sys:String>Add</sys:String>
<sys:String>Delete</sys:String>
<sys:String>Edit</sys:String>
<sys:String>Navigation</sys:String>
<sys:String>None</sys:String>
</ComboBox>
<CheckBox Content="IsReadOnly" x:Name="cbIsEditable" Click="IsEditable_Click" Margin="4" />
</StackPanel>
<dataform:DataForm x:Name="dataForm" Width="350" ItemsSource="{Binding}" HorizontalAlignment="Left" MaxWidth="500" Margin="4" Grid.Column="1" />
</Grid>
</StackPanel>
</StackPanel>
</UserControl>
4.DataFormDemo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using Silverlight.Common.Core;
namespace Silverlight.Common.View
{
public partial class DataFormDemo : UserControl
{
public DataFormDemo()
{
InitializeComponent();
DataContext = UserList.GetUserList();
}
private void IsEditable_Click(object sender, RoutedEventArgs e)
{
if ((bool)this.cbIsEditable.IsChecked)
{
this.dataForm.IsReadOnly = true;
}
else {
this.dataForm.IsReadOnly = false;
}
}
}
}
5.User.cs
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace Silverlight.Common.Core
{
public class User
{
/// <summary>
/// 用户ID
/// </summary>
public virtual int UserID { get; set; }
/// <summary>
/// 用户名
/// </summary>
public virtual string UserName { get; set; }
/// <summary>
/// 用户姓名
/// </summary>
public virtual string Name { get; set; }
/// <summary>
/// 用户密码
/// </summary>
public virtual string Password { get; set; }
/// <summary>
/// 用户描述
/// </summary>
public virtual string Description { get; set; }
/// <summary>
/// 是否启用
/// </summary>
public virtual bool IsEnabled { get; set; }
/// <summary>
/// 密码恢复问题
/// </summary>
public virtual string Question { get; set; }
/// <summary>
/// 密码恢复答案
/// </summary>
public virtual string Answer { get; set; }
}
}
6.UserList.cs
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
namespace Silverlight.Common.Core
{
public static class UserList
{
public static IList<User> GetUserList()
{
IList<User> userList = new List<User>();
userList.Add(new User { Answer="我的家乡在哪?",Description="超级管理员权限",IsEnabled=true,Name="吴建强",Password="11111",Question="山西省神池县",UserID=1,UserName="admin"});
userList.Add(new User { Answer = "我最喜欢的国家?", Description = "普通用户权限", IsEnabled = true, Name = "吴建强", Password = "sfsgfsd", Question = "韩国", UserID = 2, UserName = "lxt" });
userList.Add(new User { Answer = "我最喜欢的人?", Description = "普通用户权限", IsEnabled = true, Name = "李秀婷", Password = "11111", Question = "山西省神池县", UserID = 3, UserName = "wjq" });
userList.Add(new User { Answer = "我最喜欢的明星?", Description = "普通用户权限", IsEnabled = true, Name = "权相宇,李连杰", Password = "11111", Question = "权相宇,李连杰", UserID = 4, UserName = "qxy" });
userList.Add(new User { Answer = "我最爱看的电影?", Description = "普通用户权限", IsEnabled = true, Name = "李连杰", Password = "11111", Question = "精武英雄", UserID = 5, UserName = "lxj" });
userList.Add(new User { Answer = "我最爱看的电视剧?", Description = "普通用户权限", IsEnabled = true, Name = "权相宇", Password = "11111", Question = "《悲伤恋歌》", UserID = 6, UserName = "admin" });
userList.Add(new User { Answer = "我的梦想?", Description = "普通用户权限", IsEnabled = true, Name = "吴建强", Password = "11111", Question = "成为亿万富翁", UserID = 7, UserName = "llj" });
userList.Add(new User { Answer = "我最喜欢事?", Description = "普通用户权限", IsEnabled = true, Name = "吴建强", Password = "11111", Question = "编程", UserID = 8, UserName = "admin" });
userList.Add(new User { Answer = "我的家乡在哪?", Description = "普通用户权限", IsEnabled = true, Name = "吴建强", Password = "11111", Question = "山西省神池县", UserID = 9, UserName = "admin" });
userList.Add(new User { Answer = "我的家乡在哪?", Description = "普通用户权限", IsEnabled = true, Name = "吴建强", Password = "11111", Question = "山西省神池县", UserID = 10, UserName = "admin" });
userList.Add(new User { Answer = "我的家乡在哪?", Description = "普通用户权限", IsEnabled = true, Name = "吴建强", Password = "11111", Question = "山西省神池县", UserID = 11, UserName = "admin" });
userList.Add(new User { Answer = "我最喜欢事?", Description = "普通用户权限", IsEnabled = true, Name = "吴建强", Password = "11111", Question = "编程", UserID = 12, UserName = "admin" });
userList.Add(new User { Answer = "我的家乡在哪?", Description = "普通用户权限", IsEnabled = true, Name = "吴建强", Password = "11111", Question = "山西省神池县", UserID = 13, UserName = "admin" });
userList.Add(new User { Answer = "我的家乡在哪?", Description = "普通用户权限", IsEnabled = true, Name = "吴建强", Password = "11111", Question = "山西省神池县", UserID = 14, UserName = "admin" });
userList.Add(new User { Answer = "我的家乡在哪?", Description = "普通用户权限", IsEnabled = true, Name = "吴建强", Password = "11111", Question = "山西省神池县", UserID = 15, UserName = "admin" });
return userList;
}
}
}
注:使用DataPager无需我们在写分页代码,这样大大减少了我们的代码量,只需绑定数据源。
DataForm可以将数据保存到数据库中,这个例子中没有实现此功能,往后会逐一完善。