none
wpf scrollviewer在scrollbar上的事件获取不到 RRS feed

  • 问题

  • 我有一个scrollviewer想获取鼠标或者触笔在它的右侧scrollbar的down、up事件,该如何做。

    我使用了style、以及获取内部scrollbar等方法均无法实现。

    谢谢

    2014年3月16日 6:57

答案

  • Okay,我们可以在Load事件中定位到ScrollViewer中的ScrollBar,然后添加PreviewMouseDownPreviewMouseUp 事件:

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
                ScrollBar s = myScrollViewer.Template.FindName("PART_VerticalScrollBar", myScrollViewer) as ScrollBar;
                if (s != null)
                {
                    s.PreviewMouseDown += myScrollBar_PreviewMouseDown;
                    s.PreviewMouseUp += myScrollBar_PreviewMouseUp;
                }
    }
    
    private void myScrollBar_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
                ;
    }
    
    private void myScrollBar_PreviewMouseUp(object sender, MouseButtonEventArgs e)
    {
                ;
    }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已标记为答案 skczjun 2014年3月20日 5:06
    2014年3月19日 2:22
    版主

全部回复

  • 你好,

    一般情况下,我们只需要处理ScrollChanged事件即可,在我这边,测试了使用触摸和鼠标下的右侧scrollbar down/up动作,这个事件均可以获取。

    我的XAML代码如下:

    <Window.Resources>
            <x:Array x:Key="TestArray" Type="{x:Type sys:String}">
                <sys:String>TEST 1</sys:String>
                <sys:String>TEST 2</sys:String>
                <sys:String>TEST 3</sys:String>
                <sys:String>TEST 4</sys:String>
                <sys:String>TEST 5</sys:String>
                <sys:String>TEST 6</sys:String>
                <sys:String>TEST 7</sys:String>
                <sys:String>TEST 8</sys:String>
                <sys:String>TEST 9</sys:String>
                <sys:String>TEST 10</sys:String>
            </x:Array>
        </Window.Resources>
        <Grid>
            <ScrollViewer Height="180" ScrollChanged="ScrollViewer_ScrollChanged">
    
                <StackPanel>
                    <ItemsControl ItemsSource="{StaticResource TestArray}"
                                      FontSize="16" />
                </StackPanel>
            </ScrollViewer>
        </Grid>

    如果需要进一步帮助,请提供足够的可还原的代码帮助我们重现问题。


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年3月17日 9:34
    版主
  • 能贴一下你的获取触摸和鼠标下的右侧scrollbar down/up的实现吗?我对照着看下,谢谢
    2014年3月17日 12:18
  • 基于第一条回复中的代码,我添加了一个TextBlock去显示向上/向下,利用ScrollChangedEventArgs的VerticalChange属性,负数表示向上滚动:

    private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
    {
                if (e.VerticalChange < 0)
                    txb.Text = "Up";
                else
                    txb.Text = "Down";
    }

    截图:


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年3月18日 3:15
    版主
  • 基于第一条回复中的代码,我添加了一个TextBlock去显示向上/向下,利用ScrollChangedEventArgs的VerticalChange属性,负数表示向上滚动:

    private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
    {
                if (e.VerticalChange < 0)
                    txb.Text = "Up";
                else
                    txb.Text = "Down";
    }

    截图:


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    我没有说全,我说的是在scrollbar上的LeftMouseDown、LeftMouseUp、TouchDown、TouchUp的事件

    不是向上、向下滚动
    2014年3月19日 0:59
  • Okay,我们可以在Load事件中定位到ScrollViewer中的ScrollBar,然后添加PreviewMouseDownPreviewMouseUp 事件:

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
                ScrollBar s = myScrollViewer.Template.FindName("PART_VerticalScrollBar", myScrollViewer) as ScrollBar;
                if (s != null)
                {
                    s.PreviewMouseDown += myScrollBar_PreviewMouseDown;
                    s.PreviewMouseUp += myScrollBar_PreviewMouseUp;
                }
    }
    
    private void myScrollBar_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
                ;
    }
    
    private void myScrollBar_PreviewMouseUp(object sender, MouseButtonEventArgs e)
    {
                ;
    }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已标记为答案 skczjun 2014年3月20日 5:06
    2014年3月19日 2:22
    版主
  • PreviewMouseDownPreviewMouseUp事件可以触发,但是MouseDownMouseUp均没有触发,被丢弃了。

    如不行,就采用PreviewMouseDownPreviewMouseUp吧

    谢谢

    2014年3月20日 5:05