wpfdatagridcelleditmode

WPF dataGgridCell Edit Mode


I have requirement to keep datagridcell always in edit mode. I dont find any option to make datagrid cell edit mode so I took TextBox under datagrid cell using control template.

I am able to write in text box but datagrid cell content never get updated. How I can update datagrid cell content with whatever I write in textbox ? Here is style :

    <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>

Thanks Dee


Solution

  • Have you tried DataGridCell.IsEditing Property?

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

    Edit:

    You can keep in edit mode setting Cancel property in DataGrid.CellEditEnding Event to True.

    <DataGrid CellEditEnding="DataGrid_CellEditEnding"
    

    .

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