locked
How to disable ListBox auto scroll on mouse down and auto scrollintoview on item selected? RRS feed

  • Question

  • Hi,

          The application I am building requires me to disable the listbox auto scroll function when user mouse down and drag on the edge of the listbox. Also when a partially visible listbox item is selected, the default behaviour is that the whole selected listbox item will be scrolled into view. I wish to disable that as well.

     

         Any advices or hints will be appreciated. Thanks.

    Friday, May 11, 2007 10:05 AM

Answers

  • Ok, this is what I found to disable the listbox auto scroll function when user mouse down and drag on the edge of the listbox. First create a listbox deriving from System.Windows.Controls.ListBox. Then override the following virtual functions.

    Code Snippet

    protected override void OnPreviewMouseMove(MouseEventArgs e)
    {
        base.OnPreviewMouseMove(e);
        // Disable scrollviewer auto scrolling when dragging occurs at listbox edge
        e.Handled = true;
    }

    protected override void OnIsMouseCapturedChanged(DependencyPropertyChangedEventArgs e)
    {
         //Do not call the base function to disable item selection changes when dragging   

    //occurs at listbox edge

        //base.OnIsMouseCapturedChanged(e);
    }


    Comments and improvements are welcome. Hope this helps.



    Thursday, August 16, 2007 10:28 AM

All replies

  • bump....any hints?
    Friday, May 18, 2007 2:16 AM
  • last try? Any advices or hints will be appreciated. Thanks.
    Tuesday, May 22, 2007 10:13 AM
  • bump...
    Monday, July 9, 2007 10:19 AM
  • help!...
    Thursday, July 12, 2007 1:49 AM
  • anyone can help me??
    Thursday, August 16, 2007 9:23 AM
  • Ok, this is what I found to disable the listbox auto scroll function when user mouse down and drag on the edge of the listbox. First create a listbox deriving from System.Windows.Controls.ListBox. Then override the following virtual functions.

    Code Snippet

    protected override void OnPreviewMouseMove(MouseEventArgs e)
    {
        base.OnPreviewMouseMove(e);
        // Disable scrollviewer auto scrolling when dragging occurs at listbox edge
        e.Handled = true;
    }

    protected override void OnIsMouseCapturedChanged(DependencyPropertyChangedEventArgs e)
    {
         //Do not call the base function to disable item selection changes when dragging   

    //occurs at listbox edge

        //base.OnIsMouseCapturedChanged(e);
    }


    Comments and improvements are welcome. Hope this helps.



    Thursday, August 16, 2007 10:28 AM
  • Hello guys. This code snippet is not working :(

    Do you have some any idea about this topic.

    Monday, April 12, 2010 12:59 AM
  • Just subscribe for the RequestBringIntoView event for each of your ListBox items and set the Handled property of the RequestBringIntoViewEventArgs object to "True", like this:

    void lstBoxItem_RequestBringIntoView(object sender, RequestBringIntoViewEventArgs e) 
    { 
      e.Handled = true; 
    }

    • Proposed as answer by Sharpedia Tuesday, April 27, 2010 10:12 AM
    Tuesday, April 27, 2010 9:16 AM