单击拖动滚动数据网格

Scroll over datagrid with click drag

我有一个数据网格,它由 xml 文件加载...没问题。

当用户拖动所有数据网格或 select 行并向上或向下拖动以滚动内容时,我需要允许滚动。 我有滚动条,但是这个应用程序是用于触摸屏的,所以更容易在数据网格上拖动来滚动。

谢谢...

这应该可以解决问题。处理数据网格的 拖动 事件并以编程方式滚动数据网格。

// hook up the row events during the loading row event for the datagrid
   private void MyDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
   {
        e.Row.DragOver += new DragEventHandler(MyRow_DragOver);
        e.Row.Drop += new DragEventHandler(MyRow_Drop);
   }

   private void MyRow_DragOver(object sender, DragEventArgs e)
   {
        DataGridRow r = (DataGridRow)sender;
        MyDataGrid.SelectedItem = r.Item;

        // Scroll while dragging...
        DependencyObject d = MyDataGrid;
        while (!(d is ScrollViewer))
        {
            d = VisualTreeHelper.GetChild(d, 0);
        }
        ScrollViewer scroll = d as ScrollViewer;

        Point position = MyDataGrid.PointToScreen(Mouse.GetPosition(r));

        // create this textbox to test in real time the value of gridOrigin
        textBox1.Text = position.ToString();

        double topMargin = scroll.ActualHeight * .15;
        double bottomMargin = scroll.ActualHeight * .80;

        if (position.Y * -1 < topMargin)
         scroll.LineUp(); // <-- needs a mechanism to control the speed of the scroll.
        if (position.Y * -1 > bottomMargin)
         scroll.LineDown(); // <-- needs a mechanism to control the speed of the scroll.
    }