注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

天涯倦客的博客

祝福你朋友永远快乐!

 
 
 

日志

 
 

Silverlight4中DataGrid全选与单选的实现  

2011-09-16 14:46:57|  分类: Silverlight |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

全选/取消:

<UserControl x:Class="SilverlightApplication1.MainPage2"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<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>
<sdk:DataGrid x:Name="ContentGrid" Grid.Row="1" AutoGenerateColumns="False">
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn Header="选择">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox x:Name="ck" Tag="{Binding ID}" ></CheckBox>
</StackPanel>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTextColumn Header="编号" Binding="{Binding ID}" />
<sdk:DataGridTextColumn Header="名称" Binding="{Binding Name}" />
<sdk:DataGridTextColumn Header="年龄" Binding="{Binding age}" />
<sdk:DataGridTextColumn Header="性别" Binding="{Binding sex}" />
<sdk:DataGridTextColumn Header="时间" Binding="{Binding AddTime}" />
</sdk:DataGrid.Columns>
</sdk:DataGrid>
<Button Content="查看选中项" Grid.Row="2" Width="60" Click="Button_Click"></Button>
</Grid>
</Grid>
</UserControl>

public partial class MainPage2 : UserControl
{
public MainPage2()
{
InitializeComponent();
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 != "")
MessageBox.Show("你选中学生ID号有" + strID.Substring(0, strID.Length - 1));
else
MessageBox.Show("请选择学生");
}

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; }
}
}

------------------------------------------------------------------------------------------------------------------------------------

单选:

<UserControl x:Class="SilverlightApplication1.MainPage3"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<sdk:DataGrid x:Name="ContentGrid" Grid.Row="1" AutoGenerateColumns="False">
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn Header="选择">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<RadioButton x:Name="ck" HorizontalAlignment="Center" Click="ck_Click" Tag="{Binding ID}" GroupName="SelectedItem"></RadioButton>
</StackPanel>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTextColumn Header="编号" Binding="{Binding ID}" />
<sdk:DataGridTextColumn Header="名称" Binding="{Binding Name}" />
<sdk:DataGridTextColumn Header="年龄" Binding="{Binding age}" />
<sdk:DataGridTextColumn Header="性别" Binding="{Binding sex}" />
<sdk:DataGridTextColumn Header="时间" Binding="{Binding AddTime}" />
</sdk:DataGrid.Columns>
</sdk:DataGrid>
<Button Content="查看选中项" Grid.Row="2" Width="60" Click="Button_Click"></Button>
</Grid>
</UserControl>

public partial class MainPage3 : UserControl
{
public MainPage3()
{
InitializeComponent();
ContentGrid.ItemsSource = SetStudentInfo();
}

//查看选中项
private void Button_Click(object sender, RoutedEventArgs e)
{
string strID = "";
foreach (object ovj in ContentGrid.ItemsSource)
{
RadioButton cb1 = ContentGrid.Columns[0].GetCellContent(ovj).FindName("ck") as RadioButton;

if (cb1.IsChecked.Value)
strID = cb1.Tag.ToString();
}
if (strID != "")
MessageBox.Show("你选中学生ID号有" + strID);
else
MessageBox.Show("请选择学生");
}

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; }
}

private void ck_Click(object sender, RoutedEventArgs e)
{
foreach (object ovj in ContentGrid.ItemsSource)
{
RadioButton cb1 = ContentGrid.Columns[0].GetCellContent(ovj).FindName("ck") as RadioButton;
cb1.IsChecked = false;
}
RadioButton rbtn = sender as RadioButton;
rbtn.IsChecked = true;
}
}

转自:http://www.cnblogs.com/chuncn/archive/2010/06/05/1752191.html

  评论这张
 
阅读(1172)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017