How to adjust the size of the column in a DataGrid when the content is smaller thant the actual width of the column?

ComptonAlvaro 166 Reputation points
2021-10-25T12:00:53.263+00:00

I have a datagrid with a column. I am using a multivalue converter to set the width of the textblock of this column. The converter is this:

                <DataGridTextColumn.ElementStyle>  
                    <Style TargetType="{x:Type TextBlock}">  
                        <Setter Property="HorizontalAlignment" Value="Left"/>  
                        <Setter Property="Background" Value="Orange"/>  
                        <Setter Property="Width">  
                            <Setter.Value>  
                                <MultiBinding Converter="{StaticResource MiMultiValueConverter}">  
                                    <Binding Path="DataContext.UseConverterIsChecked" Source="{x:Reference ProxyElement}"/>  
                                    <Binding Path="DataContext.WidthColumn2" Source="{x:Reference ProxyElement}"/>  
                                </MultiBinding>  
                            </Setter.Value>  
                        </Setter>  
                    </Style>  
                </DataGridTextColumn.ElementStyle>  

I am setting the backgruond to know if it works or not.

Well, when I set a width bigger than the original width of the column, the column is resized to the size of the content. How ever, if the textblock is resized to a lower value than the actual size of the column, the column is not resize to the context.

The result is this:

Original size of the column:

143419-imagen.png

When I resize to a bigger width:

143445-imagen.png

If I resize to a lower value of the column width:

143493-imagen.png

How could I do that the width of the column adjust to the context?

Thanks.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,670 questions
{count} votes