WPF: Get an event on the scrollbar from Scrollviewer

烈酒焚心 提交于 2019-12-20 04:13:26

问题


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>

回答1:


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.



来源:https://stackoverflow.com/questions/16963003/wpf-get-an-event-on-the-scrollbar-from-scrollviewer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!