none
datagrid 获取纵向滚动条的滚动事件 RRS feed

答案

  • XAML:

        

    <DataGrid x:Name="datagrid"

    ItemsSource="{Binding}"

    Width="100" Loaded="datagrid_Loaded"/>

    C#:

        private void datagrid_Loaded(object sender, RoutedEventArgs e)
        {
          ScrollViewer scrollviewer = GetVisualChild<ScrollViewer>(datagrid);
          scrollviewer.ScrollChanged += new ScrollChangedEventHandler(scrollviewer_ScrollChanged);
        }
        void scrollviewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {
          if (e.VerticalChange != 0)
          {
            //TO DO
          }
          else return;
        }
        private T GetVisualChild<T>(Visual parent) where T : Visual
        {
          T child = default(T);
          int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
          for (int i = 0; i < numVisuals; i++)
          {
            Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
            child = v as T;
            if (child == null)
              child = GetVisualChild<T>(v);
            if (child != null)
              break;
          }
          return child;
        }


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us



    2012年5月8日 3:33
    版主

全部回复

  • XAML:

        

    <DataGrid x:Name="datagrid"

    ItemsSource="{Binding}"

    Width="100" Loaded="datagrid_Loaded"/>

    C#:

        private void datagrid_Loaded(object sender, RoutedEventArgs e)
        {
          ScrollViewer scrollviewer = GetVisualChild<ScrollViewer>(datagrid);
          scrollviewer.ScrollChanged += new ScrollChangedEventHandler(scrollviewer_ScrollChanged);
        }
        void scrollviewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {
          if (e.VerticalChange != 0)
          {
            //TO DO
          }
          else return;
        }
        private T GetVisualChild<T>(Visual parent) where T : Visual
        {
          T child = default(T);
          int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
          for (int i = 0; i < numVisuals; i++)
          {
            Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
            child = v as T;
            if (child == null)
              child = GetVisualChild<T>(v);
            if (child != null)
              break;
          }
          return child;
        }


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us



    2012年5月8日 3:33
    版主
  • 唉...不知道说我自己什么好了...

    2012年5月8日 6:15
  • 谢谢...
    2012年5月8日 6:15