C# WPF DataGrid使用,包括隔行多选

C# WPF DataGrid使用,包括隔行多选

    • xaml
    • cs

xaml

                
                
                
                    
                        
                        
                        
                    
                    
                        
                    
                    
                        
                    
                
                
            
        

cs

        /// 
        /// 单元格改变事件
        /// 
        /// 
        /// 
        private void DataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            DataGrid dataGrid = (DataGrid)sender;
            int selectIndex = dataGrid.SelectedIndex;
            var cntr =  dataGrid.ItemContainerGenerator.ContainerFromIndex(selectIndex);
            DataGridRow ObjRow = (DataGridRow)cntr;
            FrameworkElement objElement = dataGrid.Columns[0].GetCellContent(ObjRow);
            System.Windows.Controls.CheckBox objChk = (System.Windows.Controls.CheckBox)objElement;
            if (objChk.IsChecked == true) {
                objChk.IsChecked = false;
            }
            else
            {
                objChk.IsChecked = true;
            }
        }

        /// 
        /// 建了一个CheckBox,选中则为全选表格,不选中则为不全选表格
        /// 
        /// 
        /// 
        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            if ((bool)check_all.IsChecked)
            {
                //全选
                for (int i = 0; i < this.dataGrid.Items.Count; i++)
                {
                    var cntr = dataGrid.ItemContainerGenerator.ContainerFromIndex(i);
                    DataGridRow ObjROw = (DataGridRow)cntr;
                    if (ObjROw != null)
                    {
                        FrameworkElement objElement = dataGrid.Columns[0].GetCellContent(ObjROw);
                        if (objElement != null)
                        {
                            //if (objElement.GetType().ToString().EndsWith("cRUID"))
                            //{
                            System.Windows.Controls.CheckBox objChk = (System.Windows.Controls.CheckBox)objElement;
                            if (objChk.IsChecked == false)
                            {
                                objChk.IsChecked = true;
                            }
                            //}
                        }
                    }
                }
            }
            else
            {
                //全不选
                for (int i = 0; i < this.dataGrid.Items.Count; i++)
                {
                    var cntr = dataGrid.ItemContainerGenerator.ContainerFromIndex(i);
                    DataGridRow ObjROw = (DataGridRow)cntr;
                    if (ObjROw != null)
                    {
                        FrameworkElement objElement = dataGrid.Columns[0].GetCellContent(ObjROw);
                        if (objElement != null)
                        {
                            //if (objElement.GetType().ToString().EndsWith("cRUID"))
                            //{
                            System.Windows.Controls.CheckBox objChk = (System.Windows.Controls.CheckBox)objElement;
                            if (objChk.IsChecked == true)
                            {
                                objChk.IsChecked = false;
                            }
                            //}
                        }
                    }
                }
            }
        }

        #region 触摸屏上下滑动
        /// 
        /// scrollviewer 触摸屏上下滑动
        /// 
        double mPointY;//触摸点的Y坐标
        double mOffsetY;//滚动条当前位置
        bool mIsTouch = false;//是否触摸
        private void ScrollViewer_TouchDown(object sender, TouchEventArgs e)
        {
            mIsTouch = true;//正在触摸
            mOffsetY = this.scrollviewer.VerticalOffset;//获取ScrollViewer滚动条当前位置
            TouchPoint point = e.GetTouchPoint(scrollviewer);//获取相对于ScrollViewer的触摸点位置
            mPointY = point.Position.Y;//触摸点的Y坐标
        }

        private void ScrollViewer_TouchMove(object sender, TouchEventArgs e)
        {
            if (mIsTouch == true)//如果正在触摸
            {
                TouchPoint point = e.GetTouchPoint(scrollviewer);//获取相对于ScrollViewer的触摸点位置
                double DiffOffset = point.Position.Y - mPointY;//计算相对位置
                this.scrollviewer.ScrollToVerticalOffset(mOffsetY - DiffOffset);//ScrollViewer滚动到指定位置
            }
        }

        private void ScrollViewer_TouchUp(object sender, TouchEventArgs e)
        {
            mIsTouch = false;//触摸结束
        }
        #endregion

你可能感兴趣的:(C#,WPF)