DataGrid双击选中单元格该为单击

本文介绍如何在数据网格中实现单击编辑模式,包括应用到所有单元格和特定单元格的方法。

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

By default, end users must double-click a cell in the DataGrid to enter edit mode. In certain cases, it may be more desirable for cells to enter edit mode with a single mouse click (for example, when editing a CheckBox). Use the code snippets below to implement single-click editing in your DataGrid.

To apply single-click editing to all cells in the DataGrid
1. Paste the style below into the Resources of your DataGrid
2. Paste the method into the code behind

To apply single-click editing to only certain cells in the DataGrid
1. Set an x:Key on the Style (ex. <Style x:Key="SingleClickEditing" TargetType="{x:Type dg:DataGridCell}">)
2. Paste the style into the Resources of your DataGrid
3. Apply the style to the CellStyle property of the columns which you'd like to have single click editing (ex. <dg:DataGridCheckBoxColumn CellStyle="{StaticResource SingleClickEditing}"/>)
4. Paste the method into the code behind

        <!--
          SINGLE CLICK EDITING
        -->
        <Style TargetType="{x:Type dg:DataGridCell}">
          <EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown"></EventSetter>
        </Style>

        //
        // SINGLE CLICK EDITING
        //
        private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DataGridCell cell = sender as DataGridCell;
            if (cell != null && !cell.IsEditing && !cell.IsReadOnly)
            {
                if (!cell.IsFocused)
                {
                    cell.Focus();
                }
                DataGrid dataGrid = FindVisualParent<DataGrid>(cell);
                if (dataGrid != null)
                {
                    if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow)
                    {
                        if (!cell.IsSelected)
                            cell.IsSelected = true;
                    }
                    else
                    {
                        DataGridRow row = FindVisualParent<DataGridRow>(cell);
                        if (row != null && !row.IsSelected)
                        {
                            row.IsSelected = true;
                        }
                    }
                }
            }
        }    

        static T FindVisualParent<T>(UIElement element) where T : UIElement
        {
            UIElement parent = element;
            while (parent != null)
            {
                T correctlyTyped = parent as T;
                if (correctlyTyped != null)
                {
                    return correctlyTyped;
                }

                parent = VisualTreeHelper.GetParent(parent) as UIElement;
            }
            return null;
        } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值