Silverlight4中DataGrid全选与单选的实现

全选/取消:

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

posted on 2010-06-05 12:26  chuncn  阅读(1017)  评论(0编辑  收藏  举报

导航