单击拖动滚动数据网格

时间:2016-05-26 15:56:54

标签: wpf scroll datagrid drag

我有一个datagrid,这是由xml文件加载的......没问题。

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

...谢谢

1 个答案:

答案 0 :(得分:0)

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

// 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.
    }