0
votes

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!

2
Your sample code works. You sure? look around container of your DataGrid. - codeSetter
Set Width to Auto instead of * - Chris W.
@NETscape Even with DataGrid ColumnWidth="*" is the same. DataGridTemplateColumn overrides it with their widths. I tried adding items and is still the same. - Daniel
@ChrisW. Auto would stretch to content, and if content in first column is wide, all other would be out of screen. Also, all columns have different "x*" depending of expected data width. - Daniel
@Dipak Its container is Grid row. I tried moving into different place on the screen. In Design view it is shown correctly, each column stretched according to its width in * - Daniel

2 Answers

0
votes

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.

0
votes

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>