简体   繁体   中英

WPF datagrid only editable if double click

I have a DataGrid and I want the user to be able to edit some of the columns, but only if they double click the cell first. At the minute if they single click the cell, then start typing it goes into edit mode instantly.

I have tried to use MouseDoubleClick event and disable readonly then but unable to set this property in code behind.

Any help/other suggestions? Thanks

I solved this by setting the column attribute IsReadOnly="True".

Then hooked into the event MouseDoubleClick for every cell.

<Style TargetType="{x:Type DataGridCell}">
    <EventSetter Event="MouseDoubleClick" Handler="DataGridCell_MouseDoubleClick" />
</Style>

and in the code behind:

private void DataGridCell_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    if (sender.GetType() == typeof(DataGridCell))
    {
         DataGridCell cell = sender as DataGridCell;
         cell.IsEditing = true;
    }
}

This seems to ignore the isreadonly property and I can successfully update the property after double clicking. You can also filter out certain columns at this point by name, but I don't require this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM