1
votes

I have a problem with my DataGrid. The columns are loaded according to the widest visible cell, so if the user scrolls down and a wider cell appears, the datagrid is resized to fit that new cell width.

My user doesn't like this, so I searched for a way to fix this behavior. A simple suggestion I found is to set the column width to the widest cell width. But I'm having some trouble doing this.

If you have suggestions with that solution or have other solutions to resolve my problem, that would be good.

<Window x:Class="Win"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        SizeToContent="Width">        
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <GroupBox Grid.Row="0" Header="Seach">
            <TextBox TextChanged="TextBoxFilter_TextChanged"/>
        </GroupBox>
        <DataGrid Grid.Row="1" Name="DataGridOperations" AutoGenerateColumns="False" ColumnWidth="auto" CanUserAddRows="False" CanUserDeleteRows="False" AlternatingRowBackground="AntiqueWhite">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Date" IsReadOnly="True" Binding="{Binding Date,Mode=OneTime, StringFormat={}{0:dd/MM/yyyy hh:mm:ss}}"/>
                <DataGridTextColumn Header="Type" IsReadOnly="True" Binding="{Binding Type,Mode=OneTime}"/>
                <DataGridTextColumn Header="Etat" IsReadOnly="True" Binding="{Binding Status,Mode=OneTime}"/>
                <DataGridTextColumn Header="Name" IsReadOnly="True" Binding="{Binding Name,Mode=OneTime}"/>
                <DataGridTextColumn Header="Code" Binding="{Binding CodeProduct,Mode=OneTime}"/>
                <DataGridTextColumn Header="Version" IsReadOnly="True" Binding="{Binding VersionIndus,Mode=OneTime}"/>
            </DataGrid.Columns>
        </DataGrid>    
    </Grid>
</Window>

Ignore filter module. He is disabled on loading step. I use C# 4.0 .NET.

1

1 Answers

0
votes

What if just set the column width to *, in XAML:

 Width="*"

and create a celltemplate that allowes textwrapping (maybe using a texblock for that) This way the column width remains constant Cheers, G