Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing visibility of DataGridColumns causes "DataGridColumnHeader:nth-last-child(1)" to act unexepectedly #14695

Closed
mb-luwi opened this issue Feb 22, 2024 · 1 comment · Fixed by #15482

Comments

@mb-luwi
Copy link

mb-luwi commented Feb 22, 2024

Describe the bug

When changing IsVisible to "false" on the second last visible column the selector for DataGridColumnHeadersPresenter DataGridColumnHeader:nth-last-child(1), and when change IsVisible to "false" on third, fourth etc. the style that should be applied to the last child is applied to the 2nd last child.
Before:
image

After turning of Col1:
image

Or if you instead turn of Col0:
image

To Reproduce

Example code, axaml:

<UserControl.Styles>
	<Style Selector="DataGridColumnHeadersPresenter DataGridColumnHeader:nth-last-child(1) ContentPresenter">
		<Setter Property="Background" Value="Red"/>
	</Style>
</UserControl.Styles>
<Grid Background="Wheat">
	<DataGrid>
		<DataGrid.Columns>
			<DataGridTextColumn Header="Col0" Width="*" IsVisible="{Binding ShowCol0}"/>
			<DataGridTextColumn Header="Col1" Width="*" IsVisible="{Binding ShowCol1}"/>
			<DataGridTextColumn Header="Col2" Width="*" IsVisible="{Binding ShowCol2}"/>
			<DataGridTextColumn Header="Col3" Width="*" IsVisible="{Binding ShowCol3}"/>
		</DataGrid.Columns>
	</DataGrid>
	<ToggleButton IsChecked="{Binding ShowCol0}" Content="Col0"/>
	<ToggleButton IsChecked="{Binding ShowCol1}" Margin="50" Content="Col1"/>
	<ToggleButton IsChecked="{Binding ShowCol2}" Margin="100" Content="Col2"/>
	<ToggleButton IsChecked="{Binding ShowCol3}" Margin="150" Content="Col3"/>
</Grid>

ViewModel:

    [Reactive] public bool ShowCol0 { get; set; } = true;
    [Reactive] public bool ShowCol1 { get; set; } = true;
    [Reactive] public bool ShowCol2 { get; set; } = true;
    [Reactive] public bool ShowCol3 { get; set; } = false;

Change ShowCol1 or ShowCol0 to false to reproduce their respective problems.

Expected behavior

The last item should always be seen and be the one with red.

  • OS: Windows 11, tried on desktop and web
  • Avalonia-Version: 11.0.9
@appel1
Copy link
Contributor

appel1 commented Apr 18, 2024

In DataGrid.OnInsertedColumn_PreNotification the new column is inserted, InsertDisplayedColumnHeader, before EnsureHorizontalLayout.

The call to InsertDisplayedColumnHeader leads to DataGridColumnHeadersPresenter.InvalidateChildIndex that uses DataGridColumnCollection._columnsMap but it is not yet updated because it is updated in EnsureVisibleEdgedColumnsWidth called by EnsureHorizontalLayout.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants