<UserControl 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:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" x:Class="SilverlightApplication2.MainPage" mc:Ignorable="d" d:DesignWidth="673"> <Grid x:Name="LayoutRoot" Background="White"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="320"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="400"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Grid Background="White" Grid.Column="0" Grid.RowSpan="2"> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="auto"/> </Grid.RowDefinitions> <data:DataGrid x:Name="dataGrid1" Grid.Row="0"> <data:DataGrid.RowGroupHeaderStyles> <!-- Style for groups at top level --> <Style TargetType="data:DataGridRowGroupHeader"> <Setter Property="PropertyNameVisibility" Value="Collapsed" /> <Setter Property="Background" Value="#FF112255" /> <Setter Property="Foreground" Value="#FFEEEEEE" /> <Setter Property="SublevelIndent" Value="15" /> </Style> <!-- Style for groups under the top level --> <Style TargetType="data:DataGridRowGroupHeader"> <Setter Property="Background" Value="#44225566" /> </Style> </data:DataGrid.RowGroupHeaderStyles> </data:DataGrid> <StackPanel Grid.Row="1" Orientation="Horizontal"> <TextBox Text="50" Width="30" KeyDown="TextBox_KeyDown"/> <data:DataPager x:Name="dataPager1" PageSize="50" AutoEllipsis="True" NumericButtonCount="3" DisplayMode="FirstLastPreviousNextNumeric" IsTotalItemCountFixed="True" HorizontalAlignment="Right"/> <TextBlock x:Name="_CountTextBox" Text="" Width="300"/> </StackPanel> </Grid> <data:DataGrid x:Name="dataGrid2" Grid.Column="1" BorderThickness="2" AutoGenerateColumns="False"> <data:DataGrid.Columns> <data:DataGridTemplateColumn Header=""> <data:DataGridTemplateColumn.CellTemplate> <DataTemplate> <CheckBox Tag="{Binding}" Checked="CheckBox_Checked" Unchecked="CheckBox_Checked"/> </DataTemplate> </data:DataGridTemplateColumn.CellTemplate> </data:DataGridTemplateColumn> <data:DataGridTemplateColumn Header=""> <data:DataGridTemplateColumn.CellTemplate> <DataTemplate> <RadioButton Tag="{Binding}" GroupName="Projects" Checked="RadioCheckBox_Checked"/> </DataTemplate> </data:DataGridTemplateColumn.CellTemplate> </data:DataGridTemplateColumn> <data:DataGridTextColumn Header="ProjectName" Binding="{Binding ProjectName}"></data:DataGridTextColumn> </data:DataGrid.Columns> </data:DataGrid> </Grid> </UserControl> using System; 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.ObjectModel; using System.Windows.Data; using System.ComponentModel; using System.Collections.Generic; namespace SilverlightApplication2 { public partial class MainPage : UserControl { public MainPage() { // 为初始化变量所必需 InitializeComponent(); ObservableCollection<Task> taskList = new ObservableCollection<Task>(); // Generate some task data and add it to the task list. for (int i = 1; i <= 10000; i++) { taskList.Add(new Task() { ProjectName = "Project " + ((i % 5) + 1).ToString(), TaskName = "Task " + i.ToString(), DueDate = DateTime.Now.AddDays(i), Complete = (i % 2 == 0), Notes = "Task " + i.ToString() + " is due on " + DateTime.Now.AddDays(i) + ". Lorum ipsum..." }); } PagedCollectionView taskListView = new PagedCollectionView(taskList); taskListView.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(taskListView_CollectionChanged); this.dataGrid1.ItemsSource = taskListView; if (taskListView.CanGroup == true) { // Group tasks by ProjectName... taskListView.GroupDescriptions.Add(new PropertyGroupDescription("ProjectName")); // Then group by Complete status. taskListView.GroupDescriptions.Add(new PropertyGroupDescription("Complete")); } if (taskListView.CanSort == true) { // By default, sort by ProjectName. taskListView.SortDescriptions.Add(new SortDescription("ProjectName", ListSortDirection.Ascending)); } dataPager1.Source = taskListView; ObservableCollection<TaskProject> list1 = new ObservableCollection<TaskProject>(); for (int i = 1; i <= 6; i++) { list1.Add(new TaskProject() { ProjectName = "Project " + i.ToString(), }); } this.dataGrid2.ItemsSource = new PagedCollectionView(list1); } void taskListView_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { PagedCollectionView view = (sender as PagedCollectionView); this._CountTextBox.Text = String.Format("Count {0}, ItemCount {1}, TotalItemCount {2}", view.Count, view.ItemCount, view.TotalItemCount); } private void TextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) { if (e.Key == Key.Enter) { int pageSize = 0; if (Int32.TryParse((sender as TextBox).Text, out pageSize)) { if (pageSize != this.dataPager1.PageSize) { this.dataPager1.PageSize = pageSize; } } else { (sender as TextBox).Text = this.dataPager1.PageSize.ToString(); } } } private List<string> _CheckedProjects = new List<string>(); private void CheckBox_Checked(object sender, RoutedEventArgs e) { CheckBox cb = sender as CheckBox; TaskProject prj = cb.Tag as TaskProject; PagedCollectionView view = this.dataGrid1.ItemsSource as PagedCollectionView; ObservableCollection<Task> list1 = view.SourceCollection as ObservableCollection<Task>; if (cb.IsChecked == true && _CheckedProjects.Contains(prj.ProjectName) == false) { _CheckedProjects.Add(prj.ProjectName); } else { _CheckedProjects.Remove(prj.ProjectName); } view.Filter = null; view.Filter = new Predicate<object>(FilterTasks); } public bool FilterTasks(object t) { Task task = t as Task; return _CheckedProjects.IndexOf(task.ProjectName) != -1; } private string _CheckedProject; private void RadioCheckBox_Checked(object sender, RoutedEventArgs e) { RadioButton cb = sender as RadioButton; TaskProject prj = cb.Tag as TaskProject; PagedCollectionView view = this.dataGrid1.ItemsSource as PagedCollectionView; ObservableCollection<Task> list1 = view.SourceCollection as ObservableCollection<Task>; if (cb.IsChecked == true) { _CheckedProject = prj.ProjectName; } view.Filter = null; view.Filter = new Predicate<object>(FilterTask); } public bool FilterTask(object t) { Task task = t as Task; return _CheckedProject == task.ProjectName; } } }