Question

I have noticed DataGrid columns in my project do not stretch to DataGrid width any more. The only thing I did is changed Assmbly name. In XAML designer they are stretched.

Collapsed DataGrid Columns

    <DataGrid  x:Name="dg" Grid.Row="4" RowHeaderWidth="0" AutoGenerateColumns="False" 
     HorizontalGridLinesBrush="AliceBlue" AlternationCount="2">                

                <DataGrid.Columns>
                    <DataGridTemplateColumn Header="File"  IsReadOnly="True" Width="*" CanUserSort="True" SortMemberPath="File" >
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock  Text="{Binding Path=FileName}" MouseDown="FileName_MouseDown" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

//other columns are similar

                </DataGrid.Columns>                
            </DataGrid>

What I have tried:

  1. Rebuild solution

  2. Delete "bin" folder and rebuild

  3. I used old version which was OK, and changed Assembly name (which was only difference between backups) and I compared all files between these two versions (in codecompare and notepad++, one by one) and all files were the same but one stretches and the other doesn't. Both version too are old so I can not go back.

  4. Create new Datagrid with just ColumnWidth="*" AutogenerateColumns="True"

  5. HorizontalContentAlignment="Stretch" and HorizontalAlignment="Stretch"

I ran of ideas. Please offer any idea no matter if silly. Thanks!

Was it helpful?

Solution

I solved it:

The only difference was that I had on good version MyWindow.Show(); after initialization. I tried several times commenting and uncommenting this line and this is was the fix.

MyWindow is just name of the windows defined in xaml, and show() does not do anything since window is visible anyway.

If anyone else has this problem just add WindowName.Show() after InitializeComponent();

I pronounce it a bug unless one of you can think of the reason.

OTHER TIPS

For reference:

This case apparently occurs when the DataGrid is inside of a ScrollViewer. So, as told in this other answer, if you do not need the horizontal scrollbar, you can just disable it:

<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">

Else, you'll have to give the DataGrid a Width. You could create a dummy element as per sa_ddam213's answer in the linked post:

<ScrollViewer HorizontalScrollBarVisibility="Auto">
    <Grid x:Name="grid" MinWidth="200">
        <DataGrid Width="{Binding ElementName=grid, Path=ActualWidth}">
            <DataGrid.Columns>
                <DataGridCheckBoxColumn Header="Column A" Width="1*"/>
                <DataGridCheckBoxColumn Header="Column B" Width="1*"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</ScrollViewer>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top