HandyOrg / HandyOrg/HandyControl

Can't get the ScrollViewer of a ListBox

Open
#1,611 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
7.2k
Forks
1.2k
PR merge metrics
No merged PRs in 30d

Description

Describe the bug

I need to access the underlying ScrollViewer used by the ListBox. When I don't use HandyControl, I can access it as expected but when I use HandyControl I can't access it.

Steps to reproduce the bug
  1. Create the following extension method:
    public static T? GetChildOfType<T>(this DependencyObject dependencyObject) where T : DependencyObject
    {
        int childrenCount = VisualTreeHelper.GetChildrenCount(dependencyObject);
        for (int i = 0; i < childrenCount; i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(dependencyObject, i);
            if (child is T result)
            {
                return result;
            }

            T? grandChild = GetChildOfType<T>(child);
            if (grandChild is not null)
            {
                return grandChild;
            }
        }

        return null;
    }
  1. Create a window that has a ListBox like
        <ListBox VirtualizingStackPanel.VirtualizationMode="Recycling" Focusable="False"
                  ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                  VirtualizingPanel.IsVirtualizingWhenGrouping="True" VirtualizingPanel.ScrollUnit="Pixel"
                  ScrollViewer.CanContentScroll="True" VirtualizingPanel.IsVirtualizing="True"
                  ScrollViewer.VerticalScrollBarVisibility="Auto" VirtualizingPanel.IsContainerVirtualizable="True"
                  HorizontalContentAlignment="Stretch"
                  Background="Transparent"
                  x:Name="MyListBox">
            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="Focusable" Value="False" />
                </Style>
            </ListBox.ItemContainerStyle>
        </ListBox>
  1. On that window's code-behind override the OnSourceInitialized event like following:
    protected override void OnSourceInitialized(EventArgs e)
    {
        base.OnSourceInitialized(e);
        ScrollViewer myTextBoxScrollViewer = MyListBox.GetChildOfType<ScrollViewer>();
        myTextBoxScrollViewer!.ScrollToTop();
    }
  1. Notice how we can get the ScrollViewer of ListBox as expected.
  2. Now add HandyControl to the project and observe we cannot get the ScrollViewer anymore thus getting a NullReferenceException when we try to use it.
Expected behavior

We should be able to access the underlying ScrollViewer in the same way we can when we don't use HandyControl

Screenshots

No response

NuGet package version

None

IDE

No response

Framework type

No response

Windows version

No response

Additional context

No response

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the ListBox control template and the OnSourceInitialized reproduction described in the report, comparing the visual tree with and without HandyControl. Verify which template or styling change removes the ScrollViewer, then add a regression test or reproduction that confirms the underlying ScrollViewer remains discoverable and that the reported null access no longer occurs.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
desktop
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.