none
监听不到ScrollViewer的水平滚动事件 RRS feed

  • 问题

  • 我准备在ScrollViewer里面放一个Gridviewer控件。ScrollViewer屏幕大小,GridView不定大小

    前台布局:

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
            <ScrollViewer x:Name="ScrollViewerMain" Loaded="ScrollViewerMain_OnLoaded" Style="{StaticResource HorizontalScrollViewerStyle}">
                <GridView x:Name="GridView">
                    <GridView.ItemTemplate>
                        <DataTemplate>
                            <Grid Width="440" Height="300" Background="White"></Grid>
                        </DataTemplate>
                    </GridView.ItemTemplate>
               </GridView>
          </ScrollViewer>
     </Grid>

    后台水平滚动条注册事件:

    private void ScrollViewerMain_OnLoaded(object sender, RoutedEventArgs e)
            {
                ScrollBar scrollBar = FindVisualChildByName<ScrollBar>(ScrollViewerMain, "HorizontalScrollBar");
                scrollBar.ValueChanged += scrollBar_ValueChanged;
            }

    void scrollBar_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
            {
                Debug.WriteLine(e.NewValue);
            }

    绑定GridView数据源,并且在GridView也超出屏幕大小的时候,最外层的ScrollViewer是可以水平滚动的,但是却监听不到它的水平滚动事件了???

    怎么解决~????

    2013年11月11日 2:53

答案

  • Hi, LengxuanFei

    GridView里面有自带的ScrollViewer,
    你滑动的是GridView自带的ScrollViewer,所以你监听不到外层的Scrollviewer的水平滚动事件。你可以参考下GridViewstyles
    and templates

    如果您想监听到GridView里面的ScrollViewer的水平滚动事件,您可以参考以下代码:

      private void GridView_Loaded(object sender, RoutedEventArgs e)
             {
                 ScrollViewer scrollviewer = FindVisualChildByName<ScrollViewer>(GridView, "ScrollViewer");
                 ScrollBar scrollbar = FindVisualChildByName<ScrollBar>(scrollviewer, "HorizontalScrollBar");
                 scrollbar.ValueChanged += scrollBar_ValueChanged;
    
             }
              void scrollBar_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
            {
                Debug.WriteLine(e.NewValue);
                
            }
    祝您好运!


    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. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    2013年11月12日 2:32
    版主