locked
How to stop ScrollViewer from scrolling RRS feed

  • Question

  • Hi,

    I have this in XAML :


      <ScrollViewer HorizontalAlignment="Left" Margin="3,226,0,0" Name="Scv" VerticalAlignment="Top" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" >
                   
    <Canvas Height="100" Name="canvas1" Width="292">
                       
       <Image Canvas.Left="0" Canvas.Top="0" Height="440" Name="image1" Stretch="Fill" Width="730" />
                       
         <InkPresenter Name="inkPresenter1"></InkPresenter>
                   

         </Canvas>
               
    </ScrollViewer>

     

    The problem:

    1. How do I stop ScrollViewer from Scrolling ONCE the image has been scrolled to a desire positon for drawing. The reason I wantd to stop scrolling because whenever I draw up or down with inkPresenter, the scrollViewer follows making drawing very difficult.

     

    Thanks

     

    Monday, February 7, 2011 12:38 AM

All replies

  • Still can not figure out. I have tried these method :

    1)

     when use ScrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;

      This will cause up and down movement when I draw with InkPresenter up and down.

    The same if I use this, but it move side way.

    ScrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Disabled

     

    can anyone show me how to solve this problem?

    Thanks

     

     

     

     

     

    Tuesday, February 8, 2011 9:18 AM
  • Can any1 help ?

    1) I tried Override ScrollViewer but it only concerns KeyEvents. So, it did not work here.

    2) I found one article which I think may be helpful but I can not understand how to use it . Here it is :

     

    private void StopScrolling(ListBox listBox)
    {
     VirtualizingStackPanel panel = FindVisualChild<VirtualizingStackPanel>(listBox);

     double offset = panel.VerticalOffset;

     ScrollViewer scroller = FindVisualChild<ScrollViewer>(listBox);
     scroller.InvalidateScrollInfo();
     scroller.ScrollToVerticalOffset(offset);
    }

     

    private childItem FindVisualChild<childItem>(DependencyObject parent) where childItem : DependencyObject
    {
     for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
     {
      DependencyObject child = VisualTreeHelper.GetChild(parent, i);
      if (child != null && child is childItem)
      {
       return (childItem)child;
      }
      else
      {
       childItem childOfChild = FindVisualChild<childItem>(child);
       if (childOfChild != null)
       {
        return childOfChild;
       }
      }
     }
     return null;
    }

     

    Anyone can help?  Just can not figure how to stop the it scrolling and dont move.

     

    Thanks

     

     

    Thursday, February 10, 2011 7:20 AM
  • You can try with below code snippet. I am using same xaml which you have provided.Actually you have to find Scroll bar from scroll viewer and disable that based on some condition.

    Add reference to  System.Windows.Controls.Toolkit dll and add System.Windows.Controls.Primitives namespace.

     public partial class ScrollViewerSample : UserControl
        {
            ScrollBar scroll = null;
            public ScrollViewerSample()
            {
                InitializeComponent();
                Scv.LayoutUpdated += new EventHandler(Scv_LayoutUpdated);
            }
    
            void Scv_LayoutUpdated(object sender, EventArgs e)
            {
                Scv.LayoutUpdated -= new EventHandler(Scv_LayoutUpdated);
                scroll =(ScrollBar) Scv.GetVisualDescendants().Where(a => a.GetType() == typeof(ScrollBar)).FirstOrDefault();
                scroll.Scroll += new ScrollEventHandler(scroll_Scroll);
    
     
            }
    
            void scroll_Scroll(object sender, ScrollEventArgs e)
            {
    //Condition based on which you can disable the scroll process
                if (e.NewValue > 23)
                    scroll.IsEnabled = false;
            }
        }


     

    Thursday, February 10, 2011 8:09 AM
  • Hi,

    Thank you for helping. I followed your code but I have these errors :

     


    Error 1 'System.Windows.Controls.ScrollViewer' does not contain a definition for 'GetVisualDescendants' and no extension method 'GetVisualDescendants' accepting a first argument of type 'System.Windows.Controls.ScrollViewer' could be found (are you missing a using directive or an assembly reference?) 


    --- Have  add these references :

    using System.Windows.Controls;
    using System.Windows.Controls.Primitives;

     ScrollBar scb = null;

    I have a button to scroll the image to the point where I wanted:


    1) private void btnDraw_Click(object sender, RoutedEventArgs e)
            {
                if (btnDraw.Content.ToString() == "Vertical Draw Off")
                {
                    btnDraw.Content = "Vertical Draw On";

                       Scv.ScrollToVerticalOffset(Scv.VerticalOffset);

             }
           else
                {
                    Scv.IsEnabled = true;
                    btnDraw.Content = "Vertical Draw Off";
                   

                    Scv.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
                    Scv.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;


                }


    2) What value for this : e.NewValue ?


      void scb_Scroll(object sender, ScrollEventArgs e)
            {
               
                //Condition based on which you can disable the scroll process
                if (e.NewValue > 23)
                    scb.IsEnabled = false;
            }

     

     

    Thanks

    Thursday, February 10, 2011 8:53 PM
  • I think either you have not added System.Windows.Controls.Toolkit dll or not added System.Windows.Controls.Primitives namespace in your file.

    GetVisualDescendants extension method is in System.Windows.Controls.Primitives namespace so you should add that to use the code as i have mentioned in previous post.

    Friday, February 11, 2011 1:31 AM
  • Hi,

    Thank you for you quick reply. I checked again and again. Here my finding:

    1) In my reference folder, I have

    system.Windows and the standard ones


    2) in My Code behind, I have added these :

    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Controls.Primitives;


    I am not sure if these two mean the same thing :

    - System.Windows.Controls  is the same as System.Windows.Controls.Toolkit dll ?

    Where can I find System.Windows.Controls.Toolkit dll?  I can not find it in the Add-reference Dialogbox.

    Thanks

     

     

    Friday, February 11, 2011 3:56 AM
  • System.Windows.Control and System.Windows.Controls.Toolkit is not same.

    For  System.Windows.Control.Toolkit you have to download  and install Silverlight Toolkit.

    You can get this from http://silverlight.codeplex.com/ link's download section.

    Friday, February 11, 2011 4:23 AM
  • Hi again,

    I am a bit confused. There are two versions at the Codeplex. Which one should be used?

     

    1) Silverlight 4 Toolkit 

        Is this supported by Wp7 ? or for DeskTop ?

    2) Silverlight for Windows Phone Toolkit

       I believe this is supported by Wp7 but    Does this version has what you described?

     

    Thanks

     

    Friday, February 11, 2011 6:44 AM
  • You can download SilverlightControl toolkit April 2010 msi file from below link.

    http://silverlight.codeplex.com/releases/view/43528

    Friday, February 11, 2011 7:21 AM
  • Hi,

    I just downloaded  and Installed  the Silverlight For Windows phone toolkit.  I am very new to this toolkit thing.

    I have added the Microsoft.Phone.Controls.toolkit in my Project's reference folder.   Now I need some help

    1) What I need to add or reference in XAML

    2) What control to use ? Use <scrollViewer> or something else?

    3) Does this Phone.Controls.toolkit have the solution to my problem?

     

    If this is not the correct one. Should I download the silverlight Toolkit April Version? The same questions 1 and 2 for this.

    If you dont mind, can you just do a XAML and walk me through the 1 and 2?

    Thanks

     

     

     

    Thanks

    Friday, February 11, 2011 7:49 AM
  • Hi,

    I have download another sperate Silverlight ToolKit . But I still can not get it to work. ScrollViewer still Scroll UP and Down.

     

    --------GetVisualDescendants()

    source code From:http://silverlight.codeplex.com/SourceControl/changeset/view/61349#779713 

     

     void Scv_LayoutUpdated(object sender, EventArgs e)
            {

             Scv.LayoutUpdated -= new EventHandler(Scv_LayoutUpdated);
           
              scb = (ScrollBar)Scv.GetVisualDescendants().Where(a => a.GetType() == typeof(ScrollBar)).FirstOrDefault();
              scb.Scroll += new ScrollEventHandler(scb_Scroll);

            }

            void scb_Scroll(object sender, ScrollEventArgs e)
            {           
                //Condition based on which you can disable the scroll process
                //if (e.NewValue > 23)

                    scb.IsEnabled = false;

            }


      private void btnDraw_Click(object sender, RoutedEventArgs e)
            {
                if (btnDraw.Content.ToString() == "Drawing Off")
                {
                    btnDraw.Content = "Drawing On";

        Scv.ScrollToVerticalOffset(Scv.VerticalOffset);
                  
                    Scv.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;

                    //-----------
                    Scv.LayoutUpdated += new EventHandler(Scv_LayoutUpdated); 
               }

            }

     

    Questions :

      1)  Does not work whether include or Exclude Scv.LayoutUpdated -= new EventHandler(Scv_LayoutUpdated);

      2) How to Stop the ScrollViwer scroll up and down??

     

    Thanks

    Wednesday, February 16, 2011 9:52 PM
  • I know this is an old thread, but thought I'd clarify what to do in case anyone finds it in the future.

    If you use a 3rd-party library, often just installing the MSI (which often puts it in the GAC for you) is not enough.  You must also reference it in your project through the References folder in Visual Studio.  Even assuming you did that and overcame the reference issues, all of this was overkill and these guys should've known better than to take you down this path.  This is inherent functionality to the ScrollViewer object without need of other DLLs.

    All you have to do to stop the ScrollViewer from scrolling is to set it with .IsEnabled=false; when your condition is met, which you would add within a "ScrollChanged" event handler.

    private void scv_ScrollChanged(object sender, EventArgs e)
    {
        ScrollViewer scr = (ScrollViewer)sender;
    
        if (scr.VerticalOffset == 23D)  // <- whatever your condition was, not sure if this is what you want 
            scr.IsEnabled = false;  <-- and you do this
    }

    And you would add the event handler to the end of your XAML:

    <ScrollViewer HorizontalAlignment="Left" Margin="3,226,0,0" Name="Scv" VerticalAlignment="Top" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollChanged="scv_ScrollChanged">

    Another option - I found that if you have an animated GIF in a PictureBox in a ScrollViewer, it also becomes disabled if you disable your ScrollViewer with .IsEnabled = false.  To avoid that scenario, you can remove the EventHandler from the XAML and just add it dynamically, per your condition:

    if (Scv.VerticalOffset == 23D)
        Scv.ScrollChanged += new ScrollChangedEventHandler(StopScroll);

    And you have this event handler:

    private static void StopScroll(object sender, EventArgs e)
    {
        ScrollViewer scr = (ScrollViewer)sender;
        scr.ScrollToVerticalOffset(23D);   
    }

    The "D" is required because the number must be a double.  You can also use scr.ScrollToTop() if you just want to move it to the top and disable the scrollbar altogether by making it stay at the top, without scrolling to that offset position.

    Good luck!




    • Proposed as answer by navyjax2 Tuesday, October 18, 2016 3:03 PM
    • Edited by navyjax2 Tuesday, October 18, 2016 3:05 PM
    Tuesday, October 18, 2016 3:00 PM