user2261524
user2261524

Reputation: 415

WPF: Get an event on the scrollbar from Scrollviewer

I want get an event only if I the user drag the scrollbar left or right.

When I use a MouseClick event, it contains the whole canvas too...

I found that there is an Event Handler "ScrollChanged" but this is not really what I want because the width of my canvas grow every second by 10 and that cause 10 times per second the event ScrollChanged.

I want just get an event by draging the scrollbar with the mouse

        <ScrollViewer x:Name="coordinateScroll" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Margin="75,0,0,0" Width="1125" Height="750" Background="Transparent" MouseWheel="coordinateSystemBackground_MouseWheel" MouseDoubleClick="coordinateScroll_MouseDoubleClick " ScrollChanged="coordinateScroll_ScrollChanged" >
            <Canvas x:Name="coordinateSystem" HorizontalAlignment="Left" VerticalAlignment="Top" Cursor="Cross" UseLayoutRounding="False"  Width="1125" Height="720" Background="Transparent" MouseWheel="coordinateSystemBackground_MouseWheel" >

            </Canvas>
        </ScrollViewer>

Upvotes: 2

Views: 5235

Answers (1)

Sean Cogan
Sean Cogan

Reputation: 2586

From how I'm understanding you, you're trying to access the ScrollViewer whenever the user drags the scrollbar left or right. In order to do this, use the ScrollChanged event of the ScrollViewer. In the event handler, you'll have your sender and e arguments. To access properties of the ScrollViewer, simply cast sender as a ScrollViewer like this:

ScrollViewer currentViewer = (ScrollViewer)sender;

That should allow you to access all the information about the ScrollViewer.

If you're having a problem with the width of the Canvas firing the ScrollChanged event, then put a check in the event handler to see if the event is coming from a mouse, or from the Canvas width changing.

Upvotes: 1

Related Questions