简体   繁体   English

WPF datagrid仅在双击时才可编辑

[英]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. 我有一个DataGrid,我希望用户能够编辑某些列,但前提是他们必须首先双击该单元格。 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. 我试图使用MouseDoubleClick事件并禁用只读,但无法在后面的代码中设置此属性。

Any help/other suggestions? 有帮助/其他建议吗? Thanks 谢谢

I solved this by setting the column attribute IsReadOnly="True". 我通过设置列属性IsReadOnly =“ True”解决了这一问题。

Then hooked into the event MouseDoubleClick for every cell. 然后将每个单元格都挂接到事件MouseDoubleClick中。

<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. 这似乎忽略了isreadonly属性,双击后可以成功更新该属性。 You can also filter out certain columns at this point by name, but I don't require this. 您现在也可以按名称过滤掉某些列,但是我不需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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