DataGrid cell single click edit mode in wpf c#?

2020-08-05 10:16发布

问题:

I have DataGrid in my wpf application. It is having a few events. I need to edit the datagrid cell in single click. Presently it is altering when I double click the cell. I have taken a stab at something. However, it is not working for me. The rows and columns are not consistent. Dynamically I will create column for Datagrid.

this is my code..

Xaml:

 <DataGrid Name="dgTest" AutoGenerateColumns="True" CanUserResizeColumns="False" CanUserAddRows="False"
                  ItemsSource="{Binding NotifyOnSourceUpdated=True}" 
                  HorizontalAlignment="Left" 
                  SelectionMode="Single"
                  SelectionUnit="Cell"
                  IsSynchronizedWithCurrentItem="True"
                  CellStyle="{StaticResource DataGridBorder}"
                  CurrentCellChanged="dgTest_CurrentCellChanged"
                  CellEditEnding="dgTest_CellEditEnding"   
                  GotFocus="dgTest_GotFocus" LostFocus="dgTest_LostFocus"
                  GotKeyboardFocus="TextBoxGotKeyboardFocus" LostKeyboardFocus="TextBoxLostKeyboardFocus"
                  AutoGeneratingColumn="dgTest_AutoGeneratingColumn"/>

I had a go at adding some code to the "GotFocus" event. yet its not working for me. Any help would be truly valued.

CS:

 private void dgTest_GotFocus(object sender, RoutedEventArgs e)
    {
        // Lookup for the source to be DataGridCell
        if (e.OriginalSource.GetType() == typeof(DataGridCell))
        {
            // Starts the Edit on the row;
            DataGrid grd = (DataGrid)sender;
            grd.BeginEdit(e);
        }

    }     

回答1:

Try this:

Hook up the PreviewMouseLeftButtonDown event on your DataGrid:

    <DataGrid.Resources>
        <Style TargetType="{x:Type DataGridCell}">
            <EventSetter
                Event="PreviewMouseLeftButtonDown"
                Handeler="DataGridCell_PreviewMouseLeftButtonDown"/>
        </Style>
    </DataGrid.Resources>

Then in the code behind:

    private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        DataGridCell cell = (DataGridCell) sender;
        if (cell != null && !cell.IsEditing && !cell.IsReadOnly)
        {
            if (!cell.IsFocused)
            {
                cell.Focus(); 
            }
            if (grdData.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;
    }