views:

131

answers:

1

Hi,

Can someone please tell me how to hit test the scrollbars of a scrollviewer in WPF?

Thanks,

Andy

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel>
        <ScrollViewer Name="myScrollViewer" Width="280" Height="200" MouseMove="ScrollViewer_MouseMove" >


        </ScrollViewer>

        <TextBox Name="myTextBox"></TextBox>
    </StackPanel>
</Window>

Code behind ...

using System.Windows.Input;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void ScrollViewer_MouseMove(object sender, MouseEventArgs e)
        {
            myTextBox.Text = e.GetPosition(myScrollViewer).X + "," + e.GetPosition(myScrollViewer).Y;
        }

    }
}
A: 

With VisualTreeHelper.HitTest, the MouseEventArgs is relatif to the scrollviewer, so the first parameter is your ScrollViewer. Then use PointHitTestParameter with the coordinate of the MouseEventArgs.

Nicolas Dorier
Thanks for that
Andy Clarke