(xaml, wpf) ScrollViewer Binding confusion (beginner)
-
(shrug, should have posted this here before, newbie error) I have a UI with a scrollviewer like this: I want to scroll the viewer to the top when the ItemSource changes.
public partial class MyPage: UserControl
{
public MyPage()
{
InitializeComponent();
}private void Containers\_OnTargetUpdated(object? Sender, DataTransferEventArgs E) { var MyScroller = (ScrollViewer) this.FindName("Scroller"); MyScroller.ScrollToHome(); }
}
This works but it's probably not the best way to do it (ie. finding a control by name). Can I pass the ScrollViewer "object" to the TargetUpdated callback/event ? so that I don't have to referencing it by name? It seems clunky. I don't know exactly what magic incantations I need to google for. Thanks. M.
CI/CD = Continuous Impediment/Continuous Despair
-
(shrug, should have posted this here before, newbie error) I have a UI with a scrollviewer like this: I want to scroll the viewer to the top when the ItemSource changes.
public partial class MyPage: UserControl
{
public MyPage()
{
InitializeComponent();
}private void Containers\_OnTargetUpdated(object? Sender, DataTransferEventArgs E) { var MyScroller = (ScrollViewer) this.FindName("Scroller"); MyScroller.ScrollToHome(); }
}
This works but it's probably not the best way to do it (ie. finding a control by name). Can I pass the ScrollViewer "object" to the TargetUpdated callback/event ? so that I don't have to referencing it by name? It seems clunky. I don't know exactly what magic incantations I need to google for. Thanks. M.
CI/CD = Continuous Impediment/Continuous Despair
Already answered in the C# forum: Re: (xaml, wpf) ScrollViewer Binding confusion (beginner) - C# Discussion Boards[^]
private void Containers_OnTargetUpdated(object? sender, DataTransferEventArgs e)
{
var current = sender as DependencyObject;
var scroller = current as ScrollViewer;
while (scroller == null && current != null)
{
current = VisualTreeHelper.GetParent(current);
scroller = current as ScrollViewer;
}
if (scroller != null)
{
scroller.ScrollToHome();
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
(shrug, should have posted this here before, newbie error) I have a UI with a scrollviewer like this: I want to scroll the viewer to the top when the ItemSource changes.
public partial class MyPage: UserControl
{
public MyPage()
{
InitializeComponent();
}private void Containers\_OnTargetUpdated(object? Sender, DataTransferEventArgs E) { var MyScroller = (ScrollViewer) this.FindName("Scroller"); MyScroller.ScrollToHome(); }
}
This works but it's probably not the best way to do it (ie. finding a control by name). Can I pass the ScrollViewer "object" to the TargetUpdated callback/event ? so that I don't have to referencing it by name? It seems clunky. I don't know exactly what magic incantations I need to google for. Thanks. M.
CI/CD = Continuous Impediment/Continuous Despair
I don't understand your apprehension about the "direct" approach ... or is this another "MVVM" thing?
private void Containers_OnTargetUpdated(object? Sender, DataTransferEventArgs E) {
this.Scroller.ScrollToHome();
}
The compiler recognize the name you gave to the ScrollViewer in the XAML (for that page / control); you don't need "reflection" in this case. For other "public" cases, add a public "getter" for the control in question. I assume you have some idea when the ItemsSource to the list is changed; set the scroll then if event handling is too confusing.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food