Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. (xaml, wpf) ScrollViewer Binding confusion (beginner)

(xaml, wpf) ScrollViewer Binding confusion (beginner)

Scheduled Pinned Locked Moved WPF
wpfcsharpwcfdesigndevops
3 Posts 3 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Maximilien
    wrote on last edited by
    #1

    (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

    Richard DeemingR L 2 Replies Last reply
    0
    • M Maximilien

      (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

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      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

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      • M Maximilien

        (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

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups