(xaml, wpf) ScrollViewer Binding confusion (beginner) (ANSWERED)
-
SEE AND CONTINUE DISCUSSION on the WPF Forum 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.
CI/CD = Continuous Impediment/Continuous Despair
-
SEE AND CONTINUE DISCUSSION on the WPF Forum 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.
CI/CD = Continuous Impediment/Continuous Despair
-
SEE AND CONTINUE DISCUSSION on the WPF Forum 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.
CI/CD = Continuous Impediment/Continuous Despair
The
sender
parameter should be theItemsControl
, in which case you just need to walk up the visual tree to find theScrollViewer
.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
-
(obviously, I should have seen that forum) thanks.
CI/CD = Continuous Impediment/Continuous Despair
-
The
sender
parameter should be theItemsControl
, in which case you just need to walk up the visual tree to find theScrollViewer
.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
Thanks, it works. (sorry if I did not answer before). :rose:
CI/CD = Continuous Impediment/Continuous Despair