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. C#
  4. (xaml, wpf) ScrollViewer Binding confusion (beginner) (ANSWERED)

(xaml, wpf) ScrollViewer Binding confusion (beginner) (ANSWERED)

Scheduled Pinned Locked Moved C#
wpfcsharpwcfdesigndevops
5 Posts 3 Posters 0 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

    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

    L Richard DeemingR 2 Replies Last reply
    0
    • M Maximilien

      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

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

      You could try the WPF forum.

      M 1 Reply Last reply
      0
      • M Maximilien

        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

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

        The sender parameter should be the ItemsControl, in which case you just need to walk up the visual tree to find the ScrollViewer.

        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

        M 1 Reply Last reply
        0
        • L Lost User

          You could try the WPF forum.

          M Offline
          M Offline
          Maximilien
          wrote on last edited by
          #4

          (obviously, I should have seen that forum) thanks.

          CI/CD = Continuous Impediment/Continuous Despair

          1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            The sender parameter should be the ItemsControl, in which case you just need to walk up the visual tree to find the ScrollViewer.

            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

            M Offline
            M Offline
            Maximilien
            wrote on last edited by
            #5

            Thanks, it works. (sorry if I did not answer before). :rose:

            CI/CD = Continuous Impediment/Continuous Despair

            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