Skip to content

Fix: RelativePanel calculation error with extensible and aligned child #19474

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

Merged
merged 2 commits into from
Aug 19, 2025

Conversation

msojocs
Copy link
Contributor

@msojocs msojocs commented Aug 16, 2025

What does the pull request do?

Fix the problem:

The DataGrid's scroll bar does not display correctly.

What is the current behavior?

Can not see the last item of DataGrid and horizontal scrollbar.

image

Decrease width, all scroll bar are lost.

image

What is the updated/expected behavior with this PR?

  1. Create a MVVM sample
  2. Modify MainWindow.axaml and MainWindowViewModel.cs as follwing code:
<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="using:AvaloniaBugDemo.ViewModels"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" d:DesignWidth="1200" d:DesignHeight="800"
        x:Class="AvaloniaBugDemo.Views.MainWindow"
        x:DataType="vm:MainWindowViewModel"
        Icon="/Assets/avalonia-logo.ico"
        Title="AvaloniaBugDemo">
    <Design.DataContext>
        <!-- This only sets the DataContext for the previewer in an IDE,
             to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
        <vm:MainWindowViewModel/>
    </Design.DataContext>
  <RelativePanel x:Name="TestRelativePanel">
    <Panel
          x:Name="Area1"
          RelativePanel.AlignTopWithPanel="True"
          RelativePanel.AlignLeftWithPanel="True"
          RelativePanel.AlignRightWithPanel="True"
          Height="100"
          Background="LightSkyBlue">
      <TextBlock
          Text="Area1"
          HorizontalAlignment="Center"
          VerticalAlignment="Center"
          />
      <!-- <Button Click="Button_OnClick">Second</Button> -->
    </Panel>
    <Panel
      x:Name="Area2"
      RelativePanel.Below="Area1"
      RelativePanel.AlignLeftWithPanel="True"
      RelativePanel.AlignBottomWithPanel="True"
      Background="DeepSkyBlue"
        Width="100"
      >
      <TextBlock
        Text="Area2"
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        Height="100"
      />
    </Panel>
    <DataGrid
        x:Name="TestGrid"
        Background="Aqua"
        ItemsSource="{Binding DataGridExample}"
        RelativePanel.Below="Area1"
        RelativePanel.RightOf="Area2"
        RelativePanel.AlignRightWithPanel="True"
        RelativePanel.AlignBottomWithPanel="True"
        IsReadOnly="True"
        Margin="0 0 0 0"
        >
      <DataGrid.Columns>
        <DataGridTextColumn Header="Id1" Width="100" Binding="{Binding Id}"></DataGridTextColumn>
        <DataGridTextColumn Header="Id2" Width="100" Binding="{Binding Id}"></DataGridTextColumn>
        <DataGridTextColumn Header="Id3" Width="100" Binding="{Binding Id}"></DataGridTextColumn>
        <DataGridTextColumn Header="Id4" Width="100" Binding="{Binding Id}"></DataGridTextColumn>
        <DataGridTextColumn Header="Id5" Width="100" Binding="{Binding Id}"></DataGridTextColumn>
        <DataGridTextColumn Header="Id6" Width="100" Binding="{Binding Id}"></DataGridTextColumn>
        <DataGridTextColumn Header="Id7" Width="100" Binding="{Binding Id}"></DataGridTextColumn>
        <DataGridTextColumn Header="Id8" Width="100" Binding="{Binding Id}"></DataGridTextColumn>
        <DataGridTextColumn Header="Id9" Width="100" Binding="{Binding Id}"></DataGridTextColumn>
        <DataGridTextColumn Header="Id10" Width="100" Binding="{Binding Id}"></DataGridTextColumn>
      </DataGrid.Columns>
    </DataGrid>
  </RelativePanel>
</Window>
using System;
using System.Linq;
using Avalonia.Collections;

namespace AvaloniaBugDemo.ViewModels;

public partial class MainWindowViewModel : ViewModelBase
{
    public string Greeting { get; } = "Welcome to Avalonia!";
    public AvaloniaList<TestItem> DataGridExample { get; } = new (Enumerable.Range(1001, 100).Select(e => new TestItem($"{e}")));

    public MainWindowViewModel()
    {
        Console.WriteLine($"Count: {DataGridExample.Count}");
    }

}

public class TestItem(string id)
{
    public string Id { get; set; } = id;
}

How was the solution implemented (if it's not obvious)?

Original logic

  1. Measure the size of DataGrid;
    In this step, availableHeight's value is wrong. availableHeight = TotalHeight = Area1.Height + RemainArea.Height
  2. Calculate DataGrid's position.

Midified logic

  1. Calculate DataGrid's position;
    availableHeight's value is right. availableHeight = TotalHeight - Area1.Height = RemainArea.Height
  2. Measure the size of DataGrid.

Other thing

I modified the code in the branch release/11.3.4 and cherry-picked the commit to the master branch;

because I can not run a program on master branch, I do not know how to solve the compile exception.

Result

image

Checklist

  • Added unit tests (if possible)? I tried, but failed. Due to DataGrid package build exception.
  • Added XML documentation to any related classes? (I don't think it's necessary)
  • Consider submitting a PR to https://github.com/AvaloniaUI/avalonia-docs with user documentation (I don't think it's necessary)

Breaking changes

No.

Obsoletions / Deprecations

No.

Fixed issues

@avaloniaui-bot
Copy link

You can test this PR using the following package version. 12.0.999-cibuild0058390-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@cla-avalonia
Copy link
Collaborator

cla-avalonia commented Aug 16, 2025

  • All contributors have signed the CLA.

@msojocs
Copy link
Contributor Author

msojocs commented Aug 16, 2025

@cla-avalonia agree

@MrJul MrJul added bug backport-candidate-11.3.x Consider this PR for backporting to 11.3 branch labels Aug 16, 2025
Copy link
Member

@MrJul MrJul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution!

This PR needs a matching unit test, failing before the changes and passing after.
I recommend using a standard element such as a Border as the child for that test.

@msojocs
Copy link
Contributor Author

msojocs commented Aug 16, 2025

OK, I will try it.

The ScrollViewer's scrollbar does not display correctly.
@msojocs msojocs force-pushed the fix/relative-panel branch from b2e3343 to ee142ff Compare August 16, 2025 16:54
@avaloniaui-bot
Copy link

You can test this PR using the following package version. 12.0.999-cibuild0058398-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

The ScrollViewer's scrollbar does not display correctly.
@msojocs msojocs force-pushed the fix/relative-panel branch from 14b0780 to dcaff98 Compare August 17, 2025 03:45
@avaloniaui-bot
Copy link

You can test this PR using the following package version. 12.0.999-cibuild0058400-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@msojocs msojocs requested a review from MrJul August 17, 2025 04:54
Copy link
Member

@MrJul MrJul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you!

@MrJul MrJul changed the title Fix: RelativePanel calculation error with DataGrid Fix: RelativePanel calculation error with extensible and aligned child Aug 19, 2025
@MrJul MrJul added this pull request to the merge queue Aug 19, 2025
Merged via the queue into AvaloniaUI:master with commit 9908523 Aug 19, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-candidate-11.3.x Consider this PR for backporting to 11.3 branch bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants