• 三、WPF 全选,反选,以及获取选中行


    页面代码

           <TextBlock>   
                    <CheckBox Name="cbAllCreate" Click="CbAllCreate_Click">All</CheckBox> 
                    <CheckBox Name="cbInverseCreate" Click="CbInverseCreate_Click">Inverse</CheckBox>
                </TextBlock>
                <DataGrid Name="dgCreateTable" AutoGenerateColumns="False" VerticalScrollBarVisibility="Visible" Height="210" EnableRowVirtualization="False">
                    <DataGrid.Columns>
                        <DataGridCheckBoxColumn Header="Chose" ></DataGridCheckBoxColumn>
                        <DataGridTextColumn Header="Name"   Binding="{Binding}" IsReadOnly="True"></DataGridTextColumn>
                    </DataGrid.Columns>
                </DataGrid>

    注:如果不加 EnableRowVirtualization="False"  全选反选会有遗漏的情况,所以一定要加上。

    代码  

          /// <summary>
            /// 全选
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void CbAllCreate_Click(object sender, RoutedEventArgs e)
            {
                CbAll(dgCreateTable, cbAllCreate);
    
            }
            /// <summary>
            /// 反选
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void CbInverseCreate_Click(object sender, RoutedEventArgs e)
            {
                Inverse(dgCreateTable, cbInverseCreate);
            }
          /// <summary>
            /// 全选
            /// </summary>
            /// <param name="dg"></param>
            /// <param name="cb"></param>
            void CbAll(DataGrid dg, CheckBox cb)
            {
                dg.IsEnabled = cb.IsChecked != true;
    
                for (int i = 0; i < dg.Items.Count; i++)
                {
                    DataGridRow neddrow = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(i);
                    //获取该行的某列
                    if (neddrow != null)
                    {
                        FrameworkElement objElement = dg.Columns[0].GetCellContent(neddrow);
                        if (objElement != null)
                        {
                            CheckBox objChk = (CheckBox)objElement;
                            objChk.IsChecked = cb.IsChecked;
                        }
                    }
                }
            }
            /// <summary>
            /// 反选
            /// </summary>
            /// <param name="dg"></param>
            /// <param name="cb"></param>
            void Inverse(DataGrid dg, CheckBox cb)
            {
                // cb.IsChecked = false;
                for (int i = 0; i < dg.Items.Count; i++)
                {
                    DataGridRow neddrow = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(i);
                    //获取该行的某列
                    if (neddrow != null)
                    {
                        FrameworkElement objElement = dg.Columns[0].GetCellContent(neddrow);
                        if (objElement != null)
                        {
                            CheckBox objChk = (CheckBox)objElement;
                            objChk.IsChecked = !objChk.IsChecked;
                        }
                    }
                }
            }

    获取datagrid中选中的行

     List<string> GetDataGridList(DataGrid dg)
            {
                List<string> list = new List<string>();
                for (int i = 0; i < dg.Items.Count; i++)
                {
                    DataGridRow neddrow = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(i);
                    //获取该行的某列
                    if (neddrow == null)
                        continue;
                    FrameworkElement objElement = dg.Columns[0].GetCellContent(neddrow);
                    if (objElement == null)
                        continue;
    
                    CheckBox objChk = (CheckBox)objElement;
                    if (objChk.IsChecked != true)
                        continue;
    
                    var obj = dg.Columns[1].GetCellContent(neddrow);
                    if (obj == null)
                        continue;
                    var tb = (TextBlock)obj;
                    if (!string.IsNullOrEmpty(tb.Text))
                    {
                        list.Add(tb.Text);
                    }
                }
                return list;
            }
  • 相关阅读:
    python_django_分页
    python_django_中间件
    python_django_静态文件
    Django项目基础配置和基本使用
    python_django__验证码
    python_django_The requested URL /sunck/login/sunck/showmain/ was not found on this server.错误
    python_django_template_url反向解析
    python_django_template模块
    Andrew Ng机器学习算法入门(一):简介
    Vue.js 条件语句
  • 原文地址:https://www.cnblogs.com/cvol/p/10647909.html
Copyright © 2020-2023  润新知