简体   繁体   中英

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM