首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在WPF DataGrid中单击编辑

在WPF DataGrid中单击编辑
EN

Stack Overflow用户
提问于 2010-08-07 02:44:47
回答 7查看 75.6K关注 0票数 97

我希望用户能够将单元格置于编辑模式,并突出显示单元格所在的行,只需单击一次。默认情况下,这是双击。

我该如何覆盖或实现它呢?

EN

回答 7

Stack Overflow用户

发布于 2013-03-05 15:31:29

Micael Bergeron的回答对我来说是一个很好的开始,我找到了一个适合我的解决方案。为了允许单击式编辑已经处于编辑模式的同一行中的单元格,我不得不稍微调整一下。使用SelectionUnit单元格对我来说是没有选择的。

我使用了DataGridCell.GotFocus事件,而不是使用仅在第一次单击行的单元格时触发的DataGridCell.Selected事件。

代码语言:javascript
复制
<DataGrid DataGridCell.GotFocus="DataGrid_CellGotFocus" />

如果这样做,您将始终具有正确的单元格并处于编辑模式,但单元格中的任何控件都不会被聚焦,这一点我是这样解决的

代码语言:javascript
复制
private void DataGrid_CellGotFocus(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);

        Control control = GetFirstChildByType<Control>(e.OriginalSource as DataGridCell);
        if (control != null)
        {
            control.Focus();
        }
    }
}

private T GetFirstChildByType<T>(DependencyObject prop) where T : DependencyObject
{
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(prop); i++)
    {
        DependencyObject child = VisualTreeHelper.GetChild((prop), i) as DependencyObject;
        if (child == null)
            continue;

        T castedProp = child as T;
        if (castedProp != null)
            return castedProp;

        castedProp = GetFirstChildByType<T>(child);

        if (castedProp != null)
            return castedProp;
    }
    return null;
}
票数 42
EN

Stack Overflow用户

发布于 2016-09-23 22:03:07

我通过添加一个触发器解决了这个问题,该触发器在鼠标悬停在DataGridCell上时将它的IsEditing属性设置为True。它解决了我的大部分问题。它也适用于组合框。

代码语言:javascript
复制
<Style TargetType="DataGridCell">
     <Style.Triggers>
         <Trigger Property="IsMouseOver" Value="True">
             <Setter Property="IsEditing" Value="True" />
         </Trigger>
     </Style.Triggers>
 </Style>
票数 5
EN

Stack Overflow用户

发布于 2016-09-25 06:47:53

基于Duš和Knežević的建议,我更喜欢这种方式。你点击一个(就是这样))

代码语言:javascript
复制
<DataGrid.Resources>

    <Style TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}">
        <Style.Triggers>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="IsMouseOver"
                                   Value="True" />
                        <Condition Property="IsReadOnly"
                                   Value="False" />
                    </MultiTrigger.Conditions>
                    <MultiTrigger.Setters>
                        <Setter Property="IsEditing"
                                Value="True" />
                    </MultiTrigger.Setters>
                </MultiTrigger>
        </Style.Triggers>
    </Style>

</DataGrid.Resources>
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3426765

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档