HandyOrg / HandyOrg/HandyControl
Can't get the ScrollViewer of a ListBox
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
- 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;
}
- 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>
- 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();
}
- Notice how we can get the ScrollViewer of ListBox as expected.
- Now add HandyControl to the project and observe we cannot get the ScrollViewer anymore thus getting a
NullReferenceExceptionwhen 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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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