tags:

views:

166

answers:

3

I want the user to be able to put the cell into editing mode and highlight the row the cell is contained in with a single click. By default, this is double click. How do I override or implement this? I've searched on google, and the codeplex answer doesn't work for me.

I'm pretty new to WPF and coding in general, so a simple answer is better.

Thanks

+1  A: 

From: http://wpf.codeplex.com/wikipage?title=Single-Click%20Editing

XAML:

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

CODE-BEHIND:

//
// 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;
}
myermian
this doesnt work in certain cases, and its more complicated then Micael Bergerons solution.
SwissCoder
A: 

I know this works with the DataGrid that comes with .Net 4.0 but not sure about the CodePlex version.

<DataGrid SelectionUnit="FullRow" />
Rachel
this does not put the cell directly into editmode..
SwissCoder
+2  A: 

Here is how I resolved this issue:

<DataGrid DataGridCell.Selected="DataGrid_GotFocus" ItemsSource="{Binding Source={StaticResource itemView}}">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Nom" Binding="{Binding Path=Name}"/>
        <DataGridTextColumn Header="Age" Binding="{Binding Path=Age}"/>
    </DataGrid.Columns>
</DataGrid>

This DataGrid is bound to a CollectionViewSource (Containing dummy Person objects).

The magic happens there : DataGridCell.Selected="DataGrid_GotFocus".

I simply hook the Selected Event of the DataGrid cell, and call BeginEdit() on the DataGrid.

Here is the code behind for the event handler :

    private void DataGrid_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);
        }
    }

{enjoy}

Micael Bergeron
First I doubted your answer, as I didn't expect the GotFocus Event to be tirggered every time I click on a Row. BUT it works great! Thank you a lot!
SwissCoder