在ASP.NET我們很經(jīng)常用到控件DataGrid或Repeater等顯示數(shù)據(jù),時(shí)常會(huì)須要加一個(gè)控件為全選或全取消的功能,,例: Code
//DataGrid for (int i = 0; i < this.GVVideoLib.Rows.Count; i++) { CheckBox cb = (CheckBox)this.GVVideoLib.Rows[i].FindControl("ckChoose");//模板中checkbox控件ID cb.Checked = true; //獲取選中的ID //if (cb.Checked) //{ // Label lb = (Label)GVVideoLib.Rows[i].FindControl("lbSelect");//lbSelect隱藏控件ID 用于存放ID // vidArr += lb.Text.Trim() + ","; //} } //Repeater foreach (RepeaterItem item in rpReview.Items) { CheckBox ckBox = (CheckBox)item.FindControl("ckChoose");//模板中checkbox控件ID ckBox.Checked = true; //獲取選中的ID //if (ckBox.Checked) //{ // Label lbMsgId = (Label)item.FindControl("lbSelect");//lbSelect隱藏控件ID 用于存放ID // sb.Append(lbMsgId.Text.Trim()).Append("|"); //} } 而在此silverlight 2.0正式版中的DataGrid控件沒有rows等屬性我們?nèi)绾稳x與取消呢,在運(yùn)用過程中做了個(gè)人小結(jié),,如果還有好方法,多多學(xué)習(xí): XAML 為 Code
<UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" x:Class="CheckBoxAllDataGrid.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="400" Height="300" Loaded="UserControl_Loaded"> <Grid x:Name="LayoutRoot" Background="White"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="30"/> <RowDefinition Height="Auto"/> <RowDefinition Height="30"/> </Grid.RowDefinitions> <CheckBox x:Name="CBAll" Grid.Row="0" Content="全選" Click="CheckBox_Click"></CheckBox> <data:DataGrid x:Name="ContentGrid" Grid.Row="1" AutoGenerateColumns="False"> <data:DataGrid.Columns> <data:DataGridTemplateColumn Header="選擇"> <data:DataGridTemplateColumn.CellTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <CheckBox x:Name="ck" Tag="{Binding ID}" ></CheckBox> </StackPanel> </DataTemplate> </data:DataGridTemplateColumn.CellTemplate> </data:DataGridTemplateColumn> <data:DataGridTextColumn Header="編號(hào)" Binding="{Binding ID}" /> <data:DataGridTextColumn Header="名稱" Binding="{Binding Name}" /> <data:DataGridTextColumn Header="年齡" Binding="{Binding age}" /> <data:DataGridTextColumn Header="性別" Binding="{Binding sex}" /> <data:DataGridTextColumn Header="時(shí)間" Binding="{Binding AddTime}" /> </data:DataGrid.Columns> </data:DataGrid> <Button Content="查看選中項(xiàng)" Grid.Row="2" Width="60" Click="Button_Click"></Button> </Grid> </Grid> </UserControl> cs.
Code
using System.Collections.Generic; using System.Windows; using System.Windows.Controls; using System.Windows.Browser; namespace CheckBoxAllDataGrid { public partial class Page : UserControl { public Page() { InitializeComponent(); } private void UserControl_Loaded(object sender, RoutedEventArgs e) { ContentGrid.ItemsSource = SetStudentInfo(); } //全選 private void CheckBox_Click(object sender, RoutedEventArgs e) { if (ContentGrid.ItemsSource != null) { if (this.CBAll.IsChecked.Value)//全選 { foreach (object ovj in ContentGrid.ItemsSource) { CheckBox cb1 = ContentGrid.Columns[0].GetCellContent(ovj).FindName("ck") as CheckBox; //cb為 cb1.IsChecked = true; } } else//取消 { foreach (object obj in ContentGrid.ItemsSource) { CheckBox cb2 = ContentGrid.Columns[0].GetCellContent(obj).FindName("ck") as CheckBox; cb2.IsChecked = false; } } } } //查看選中項(xiàng) private void Button_Click(object sender, RoutedEventArgs e) { string strID = ""; foreach (object ovj in ContentGrid.ItemsSource) { CheckBox cb1 = ContentGrid.Columns[0].GetCellContent(ovj).FindName("ck") as CheckBox; if (cb1.IsChecked.Value) strID = strID + cb1.Tag + ","; } if (strID != "") HtmlPage.Window.Alert("你選中學(xué)生ID號(hào)有" + strID.Substring(0, strID.Length - 1)); else HtmlPage.Window.Alert("請(qǐng)選擇學(xué)生"); } public List<student> SetStudentInfo() { List<student> studentinfo = new List<student>(); studentinfo.Add(new student { ID = 1, Name = "test1", age = 22, sex = "男", AddTime = "1987-01-09" }); studentinfo.Add(new student { ID = 2, Name = "李四", age = 22, sex = "男", AddTime = "1981-11-6" }); studentinfo.Add(new student { ID = 3, Name = "王五", age = 22, sex = "男", AddTime = "2001-01-9" }); studentinfo.Add(new student { ID = 4, Name = "小林", age = 22, sex = "女", AddTime = "1985-03-12" }); studentinfo.Add(new student { ID = 5, Name = "小金", age = 22, sex = "男", AddTime = "1989-01-21" }); studentinfo.Add(new student { ID = 6, Name = "小吳", age = 22, sex = "女", AddTime = "1984-08-5" }); studentinfo.Add(new student { ID = 7, Name = "小王", age = 22, sex = "男", AddTime = "1983-07-09" }); studentinfo.Add(new student { ID = 8, Name = "可耕地", age = 22, sex = "男", AddTime = "1986-09-10" }); studentinfo.Add(new student { ID = 9, Name = "測(cè)試", age = 22, sex = "男", AddTime = "1988-11-09" }); return studentinfo; } public struct student { public int ID { get; set; } public string Name { get; set; } public int age { get; set; } public string sex { get; set; } public string AddTime { get; set; } } } } |
|