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

2021-06-09 17:06

阅读:356

标签:llb   block   container   div   isnull   containe   scrollbar   lis   rtu   

页面代码

技术图片

 

       TextBlock>   
                CheckBox Name="cbAllCreate" Click="CbAllCreate_Click">AllCheckBox> 
                CheckBox Name="cbInverseCreate" Click="CbInverseCreate_Click">InverseCheckBox>
            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"  全选反选会有遗漏的情况,所以一定要加上。

 

代码  

      /// 
        /// 全选
        /// 
        /// 
        /// 
        private void CbAllCreate_Click(object sender, RoutedEventArgs e)
        {
            CbAll(dgCreateTable, cbAllCreate);

        }
        /// 
        /// 反选
        /// 
        /// 
        /// 
        private void CbInverseCreate_Click(object sender, RoutedEventArgs e)
        {
            Inverse(dgCreateTable, cbInverseCreate);
        }
      /// 
        /// 全选
        /// 
        /// 
        /// 
        void CbAll(DataGrid dg, CheckBox cb)
        {
            dg.IsEnabled = cb.IsChecked != true;

            for (int i = 0; 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;
                    }
                }
            }
        }
        /// 
        /// 反选
        /// 
        /// 
        /// 
        void Inverse(DataGrid dg, CheckBox cb)
        {
            // cb.IsChecked = false;
            for (int i = 0; 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中选中的行

 Liststring> GetDataGridList(DataGrid dg)
        {
            Liststring> list = new Liststring>();
            for (int i = 0; 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;
        }

 

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

标签:llb   block   container   div   isnull   containe   scrollbar   lis   rtu   

原文地址:https://www.cnblogs.com/cvol/p/10647909.html


评论


亲,登录后才可以留言!