WPF数据网格单元格编辑模式

3

我有一个要求,需要始终将datagridcell保持在编辑模式。我没有找到任何选项可以使datagrid单元格处于编辑模式,因此我使用控件模板在datagrid单元格下方放置了TextBox。

我能够在文本框中写入内容,但是datagrid单元格的内容从未得到更新。我该如何使用文本框中的内容更新datagrid单元格的内容?以下是样式:

    <Style TargetType="{x:Type DataGridCell}" x:Key="DatagridCellWithTextbox">
            <Setter Property="BorderThickness" Value="2"></Setter>
            <Setter Property="Foreground" Value="{DynamicResource ContentNormalBrush}"/>
            <Setter Property="Margin" Value="0"></Setter>
            <Setter Property="VerticalContentAlignment" Value="Center"></Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DataGridCell}">
                        <Grid Background="{TemplateBinding Background}">
                          <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                            <TextBox x:Name="txtCell" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.Text}" VerticalContentAlignment="Top" Focusable="True" />
                          </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
          </Style>

谢谢 Dee。
3个回答

4

您尝试过使用DataGridCell.IsEditing属性吗?

<Style TargetType="{x:Type DataGridCell}">
    <Setter Property="IsEditing" Value="True" />
</Style>

编辑:

您可以通过将DataGrid.CellEditEnding事件中的Cancel属性设置为True来保持编辑模式。

<DataGrid CellEditEnding="DataGrid_CellEditEnding"

.

private void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
    e.Cancel = true;
}

我尝试过进行编辑,但是当我完成编辑后,它又回到了非编辑模式。我希望它始终保持在编辑模式下。 - Dee
起初我认为它会恢复样式设置器,也许可以通过一个始终为真的样式触发器来完成,但最好尝试我的编辑中的新建议。 - LPL

3
我知道这个问题比较老,但我最近开发了自己的解决方案。
我的方法是继承自DataGridTextColumn并重写GenerateElement方法:
public class EditingTextBoxColumn : DataGridTextColumn
{
    protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
    {
        var textbox = base.GenerateEditingElement(cell, dataItem) as TextBox;
        textbox.IsReadOnly = IsReadOnly;
        return textbox;
    }
}

这也适用于其他类型的列。 当然,这仅适用于已经存在的DataGridColumn类型(如DataGridTextColumnDataGridComboBoxColumn等)。

3

我知道这个问题有点老了,但是我认为我有一个更好的答案。使用只读的DataGridTemplateColumn并设置CellTemplate。在CellTemplate中绑定工作不正常,但你可以通过不同的RelativeSource解决这个问题:

<DataGridTemplateColumn Width="Auto" Header="Select" IsReadOnly="True">
  <DataGridTemplateColumn.CellTemplate>
    <DataTemplate DataType="models:DealAcctListItem">
      <CheckBox IsChecked="{Binding RelativeSource=
                           {RelativeSource AncestorType=DataGridCell}, 
                           Path=DataContext.IsSelected}" />
    </DataTemplate>
  </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

这要比尝试让DataGrid的编辑模式正常工作容易得多。


1
感谢Chris发布了这篇关于一个古老问题的卓越更新,它真的对我们很有帮助。你2020年左右的解决方案比其他人都要好得多。让我给你颁发一些额外的积分。干得好。 - Thomas Weller
@ThomasWeller 很棒的举动! - BionicCode

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接