silverlight中CheckBox在DataGrid的全选/取消
在ASP.NET我们很经常用到控件DataGrid或Repeater等显示数据,时常会须要加一个控件为全选或全取消的功能,例:
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("|");
//}
}
//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等属性我们如何全选与取消呢,在运用过程中做了个人小结,如果还有好方法,多多学习:
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="编号" Binding="{Binding ID}" />
<data:DataGridTextColumn Header="名称" Binding="{Binding Name}" />
<data:DataGridTextColumn Header="年龄" Binding="{Binding age}" />
<data:DataGridTextColumn Header="性别" Binding="{Binding sex}" />
<data:DataGridTextColumn Header="时间" Binding="{Binding AddTime}" />
</data:DataGrid.Columns>
</data:DataGrid>
<Button Content="查看选中项" Grid.Row="2" Width="60" Click="Button_Click"></Button>
</Grid>
</Grid>
</UserControl>
<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="编号" Binding="{Binding ID}" />
<data:DataGridTextColumn Header="名称" Binding="{Binding Name}" />
<data:DataGridTextColumn Header="年龄" Binding="{Binding age}" />
<data:DataGridTextColumn Header="性别" Binding="{Binding sex}" />
<data:DataGridTextColumn Header="时间" Binding="{Binding AddTime}" />
</data:DataGrid.Columns>
</data:DataGrid>
<Button Content="查看选中项" 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;
}
}
}
}
//查看选中项
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("你选中学生ID号有" + strID.Substring(0, strID.Length - 1));
else
HtmlPage.Window.Alert("请选择学生");
}
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 = "测试", 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; }
}
}
}
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;
}
}
}
}
//查看选中项
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("你选中学生ID号有" + strID.Substring(0, strID.Length - 1));
else
HtmlPage.Window.Alert("请选择学生");
}
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 = "测试", 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; }
}
}
}