Closed VisualAlf closed 2 months ago
🙌 Thanks, @VisualAlf! Investigating it now!
@w-ahmad
According to the docs use of GetTemplateChild() should probably be restricted to OnAppyTemplate().
You can use the GetTemplateChild method inside your OnApplyTemplate override and keep a reference to the objects you need.
which may read as: outside of OnApplyTemplate() the function MAY work. And here we've found the case where it doesn't.
So, if you move the access to Scrollviewer into OnApplyTemplate() and later use only the reference to it, the bug is gone.
I had success with changing the methods:
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
_headerRow = GetTemplateChild("HeaderRow") as TableViewHeaderRow;
// test
if (GetTemplateChild("ScrollViewer") is ScrollViewer scrollViewer)
{
_scrollViewer = scrollViewer;
}
else
Debugger.Break();
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
//if (GetTemplateChild("ScrollViewer") is not ScrollViewer scrollViewer)
//{
// return;
//}
//_scrollViewer = scrollViewer;
// replace by:
ScrollViewer scrollViewer;
if (_scrollViewer == null)
return;
else
scrollViewer = _scrollViewer;
Canvas.SetZIndex(ItemsPanelRoot, -1);
...
You may consider -as a precaution- replace the other uses of GetTemplateChild() as well.
Thanks for your work !
agreed, GetTemplateChild("ScrollViewer")
should be moved to the OnApplyTemplate()
method.
I need to swap two panels each having a TableView on it. So I defined one panels's Visbiilty as Collapsed. When I show this collapsed panel at runtime all seems OK, until I click a cell. Then a NPE is thrown, since obviously the ScrollViewer from the template is null. Happens first in TableView.GetColumnsInDisplay().
If you look at the VisualTree just before clicking, there sure is a Scrollviewer. However when I catch the Loaded Event on the TableView, there isn't any. So this maybe a WinUI-Problem. Anyway, Imho, the TableView. OnLoaded() should not simply return if there is no scrollviewer found.
Steps to Reproduce:
Here is a simple example:
MainWindow.xaml
MainWindow.xaml.cs
Workaround:
Define both panels with Visibility="Visible". Hide tableView2 in the Loaded Event.
Expected behavior:
Obviously shouldn't throw.
Environment: