My goal is creating an inplace editor in WPF datagrid column to edit large texts.
My datasource is a DataTable that can contain data from different tables and fields, that’s why i have no any defined types to bind to. In my example it has 1 column named “Test”.
Now i wrote some XAML code to define my column:
<ControlTemplate x:Key="ExtendedTemplate">
<StackPanel>
<TextBox Text="{Binding Test}" Width="200" Height="100" AcceptsReturn="True" TextWrapping="Wrap"/>
</StackPanel>
</ControlTemplate>
<DataGrid x:Name="grid" ItemsSource="{Binding}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="TEST Column" Width="200">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox IsDropDownOpen="True">
<ComboBoxItem Template="{StaticResource ExtendedTemplate}"/>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Test}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Here is my test datasource:
Source = new DataTable("Test");
Source.Columns.Add("Test");
Source.Rows.Add("Item 1 - large amount of text ...");
Source.Rows.Add("Item 2");
Source.Rows.Add("Item 3");
grid.DataContext = Source;
This works fine, but the last thing i need to do is to decorate grid cell while it is in edit mode and i’m typing text in expanded combo:
It is important:
First – combobox isn’t binded to any ItemsSource, but single ComboBoxItem exists for any cell and contains text from that cell.
Second – i can’t define DataTemplate to SelectedItem because ComboBox.SelectionBoxItemTemplate Property is read only.
Does anybody know how can i replace datatemplate for SelectionBoxItem to something like this?
<DataTemplate>
<TextBlock Text="{Binding Test}"/>
</DataTemplate>
I tried to create custom style for combobox with command “Edit template – Edit a copy…”. There is a lot of markup and i don’t want to post it here. Here is a small part edited by me.
<ContentPresenter ContentTemplate="{StaticResource SimplestTemplate}"
ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
Content="{TemplateBinding SelectionBoxItem}"
ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}"
IsHitTestVisible="false"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"/>
It doesn’t work, because id don’t know how to write my “SimplestTemplate” that should bind data to my “Test” field.

I achieved my goal by creating a style, derived from
ComboBox, and overriding itsContentPresenterContentTemplate. It works ok, but i stopped my work on it, because there are many additional features, that need to be implemented (such as resizing popup editor, automatic grid cell refreshing, assigning commands to hotkeys to work with editor with keyboard only etc.). At last i decide to use third-party component (this one).As an answer to my question i post my final XAML markup:
Resources:
Grid column template:
To show clipped text in cell i created a simple attached property. It cuts all
"\n"symbols in text to make it single-line.