beta.blog

WPF: DataGridTextColumn resizes automatically when editing an element

by on Mar.25, 2013, under Programming

Well I’ve ran into a problem today, where a DataGridTextColumn would always resize automatically whenever I edited an element.

Original textblock:

wpf-datagridtextblock-0

Textblock after double clicking it in order to edit the element:

wpf-datagridtextblock-1

Solution:

As a solution, all I did was to add the following code inside my <DataGridTextColumn>…</DataGridTextColumn> tag:

<DataGridTextColumn>
    <DataGridTextColumn.EditingElementStyle>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="HorizontalAlignment" Value="Stretch" />
            <Setter Property="TextAlignment" Value="Left" />
            <Setter Property="Width" Value="{Binding ElementName=InstallationPathColumn, Path=ActualWidth}" />
            <Setter Property="MinWidth" Value="200" />
        </Style>
    </DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>

This way I could also style the behaviour it would follow when editing an item. It’s of utter importance to add a DataGridTextColumn.EditingElementStyle block in order to accomplish this.

Final Result:

wpf-datagridtextblock-2

Leave a Reply

*

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!