WPF DataGrid 遇到的小问题

本文介绍了在使用WPF DataGrid时遇到的双向绑定问题及获取行和单元格的方法。探讨了如何在mode为twoway绑定数据时确保数据正确更新,并分享了如何有效访问DataGrid内部数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

WPF DataGrid 遇到的小问题

        1) 在使用WPF的DataGrid时,虽然在绑定数据时使用的mode是twoway,

但在具体使用的时候,因为界面输入过快,数据的传输具有一定的延迟。
这样会有时导致异常的出现,解决这个办法我们可以自己调用函数datagrid.commiteEdit();
来手动传输数据。
        2)在输入数据后,界面没有及时的更新某些信息,如行数,以及在删除某行后,
行数没有重新排列。只要使用datagir.Items.Refresh();  就可以使得界面刷新。

       3)获得DataGrid中的某一行或者某一个单元格


        #region DataGrid Cell Row

        /// <summary>
        /// get the row of datagrid
        /// </summary>
        /// <param name="Index"></param>  [0,n-1]
        /// <returns>DataGridRow</returns> 
        private DataGridRow getRow(int Index)
        {
            try
            {
                DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(Index);
                if (row == null)
                {
                    dataGrid.UpdateLayout();
                    dataGrid.ScrollIntoView(dataGrid.Items[Index]);
                    row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(Index);
                }
                return row;
            }
            catch (Exception ex)
            {
                log.Error("getRow()----- exception occurred," + ex.Message);
                return null;
            }
        }
 
        /// <summary>
        /// get the cell of dataGrid by an existing row
        /// </summary>
        /// <param name="rowIndex"></param>  [0,n-1]
        /// <param name="columnIndex"></param>  [0,n-1]
        /// <returns>DataGridCell</returns>
        private DataGridCell getCell(int rowIndex, int columnIndex)
        {
            try
            {
                DataGridRow rowContainer = getRow(rowIndex);
               
                if (rowContainer != null)
                {
                    DataGridCellsPresenter presenter = getVisualChild<DataGridCellsPresenter>(rowContainer);
                    
                    if (presenter == null)
                    {
                        dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[columnIndex]);
                        presenter = getVisualChild<DataGridCellsPresenter>(rowContainer);
                    }
                    
                    DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
                    if (cell == null)
                    {
                        dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[columnIndex]);
                        cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
                    }

                    return cell;
                } // end of if (rowContainer != null)

            }
            catch (Exception ex)
            {
                log.Error("getCell()---- exception occurred, " + ex.Message);
            }
            return null;
        }

        /// <summary>
        /// get the visual child
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parent"></param>
        /// <returns></returns>
        private static T getVisualChild<T>(Visual parent) where T : Visual
        {
            try
            {
                T child = default(T);
                int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
                for (int i = 0; i < numVisuals; i++)
                {
                    Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
                    child = v as T;
                    if (child == null)
                    {
                        child = getVisualChild<T>(v);
                    }
                    if (child != null)
                    {
                        break;
                    }
                }
                return child;
            }
            catch (Exception ex)
            {
                log.Error("getVisualChild()---- exception occurred, " + ex.Message);
                return null;
            }
        }

        #endregion


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值