Logan B. Lehman
Logan B. Lehman

Reputation: 5007

WPF Datagrid Column Width Issue

I have columns in a DataGrid that are being set by an ObservableCollection that is the type of a simple data object that I created. The first column has a width set to "Auto" and the second column as a width set to "1*".

I am currently using the method in the answer here to autoupdate the width of my column that is set to "Auto" when the ItemsSource changes. This seems to work most of the time:

enter image description here This looks great, and works all of the time

Although, when the ItemsSource is a little bit larger (lets say about 30-35 records), the "Auto" width (first) column will shrink down only when the DataGrid (including the scroll bar) is clicked:

enter image description here This will be resized properly if it hasn't been clicked

My XAML looks like this:

 <my:DataGrid CanUserSortColumns="false" CanUserResizeRows="false" CanUserResizeColumns="false" CanUserReorderColumns="false" CanUserDeleteRows="false" CanUserAddRows="false" AutoGenerateColumns ="False" SelectionMode="Single" SelectionUnit="Cell" Height="113" HorizontalAlignment="Left" Margin="11,22,0,0" Name="dataGrid" VerticalAlignment="Top" Width="226" Background="#FFE2E2E2" AlternatingRowBackground="#FFA4CFF2" BorderBrush="#FF7C7C7C" HorizontalGridLinesBrush="White" PreviewKeyDown="dataGrid_PreviewKeyDown" CellEditEnding="dataGrid_CellEditEnding" BeginningEdit="dataGrid_BeginningEdit" PreparingCellForEdit="dataGrid_PreparingCellForEdit" SelectedCellsChanged="dataGrid_SelectedCellsChanged" Loaded="dataGrid_Loaded" TargetUpdated="dataGrid_TargetUpdated">
        <my:DataGrid.Columns>
        <my:DataGridTextColumn Binding="{Binding Path=Name, NotifyOnTargetUpdated=True}" Width="Auto">
                <my:DataGridTextColumn.CellStyle>
                    <Style TargetType="{x:Type my:DataGridCell}">
                        <Setter Property="KeyboardNavigation.IsTabStop" Value="False"></Setter>
                        <Setter Property="IsHitTestVisible" Value="False"></Setter>
                        <Setter Property="Focusable" Value="False"></Setter>
                        <Setter Property="Background" Value="WhiteSmoke"></Setter>
                        <Setter Property="BorderBrush" Value="LightGray"></Setter>
                    </Style>
                 </my:DataGridTextColumn.CellStyle>
              </my:DataGridTextColumn>
        <my:DataGridTextColumn Binding="{Binding Path=Value}" Width="1*"></my:DataGridTextColumn>
   </my:DataGrid.Columns>
 </my:DataGrid>

The code to assure the updating of the column:

 private void dataGrid_TargetUpdated(object sender, DataTransferEventArgs e)
    {
        dataGrid.Columns[0].Width = 0;
        dataGrid.UpdateLayout();
        dataGrid.Columns[0].Width = new DataGridLength(0, DataGridLengthUnitType.Auto);
        dataGrid.UpdateLayout();
    }

Is there any reason this may be happening only when the list is longer like this?

Upvotes: 3

Views: 3133

Answers (1)

denis morozov
denis morozov

Reputation: 6316

DataGrid's TargetUpdated might not get called in a few scenarios. For example, when you have more rows coming in but they are not visible then the datagrid doesn't have to "waste cycles on" re-rendering something that is not visible. The initial TargetUpdated is fine, but you might have to find an additional hook, and do similar thing there, such as hooking into the CollectionChanged of the object that's bound to ItemsSource of your datagrid, your observableCollection has the event CollectionChanged, subscribe and try your logic there.

Upvotes: 1

Related Questions