Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DataGrid,将ScrollViewer重置到左侧_C#_Wpf_Datagrid_Scrollviewer - Fatal编程技术网

C# DataGrid,将ScrollViewer重置到左侧

C# DataGrid,将ScrollViewer重置到左侧,c#,wpf,datagrid,scrollviewer,C#,Wpf,Datagrid,Scrollviewer,有没有办法将DataGrid的水平滚动查看器重置回左侧的所有位置?我把它连接到另一个控件上,在那里可以刷新绘图。如果关联的DataGrid也被重置,并且scrollViewer一直位于左侧,那就太好了。谢谢。您不能直接访问ScrollViewer,但是如果您使用snoop查看可视化树,您可以看到它看起来像DataGrid、Border和ScrollViewer 因此,要向左滚动DataGrid,可以使用以下扩展方法,也可以使用普通方法 public static class DataGridEx

有没有办法将DataGrid的水平滚动查看器重置回左侧的所有位置?我把它连接到另一个控件上,在那里可以刷新绘图。如果关联的DataGrid也被重置,并且scrollViewer一直位于左侧,那就太好了。谢谢。

您不能直接访问ScrollViewer,但是如果您使用snoop查看可视化树,您可以看到它看起来像DataGrid、Border和ScrollViewer

因此,要向左滚动DataGrid,可以使用以下扩展方法,也可以使用普通方法

public static class DataGridExtensions
{
    public static void ScrollToLeft(this DataGrid dataGrid)
    {
        Border border = VisualTreeHelper.GetChild(dataGrid, 0) as Border;
        ScrollViewer scrollViewer = border.Child as ScrollViewer;
        scrollViewer.ScrollToLeftEnd();
    }
}
这样说吧

dataGrid.ScrollToLeft();

我使用它来确保所选行的第一个单元格可见

    if (dataGrid.SelectedItem != null)
        dataGrid.ScrollIntoView(dataGrid.SelectedItem, dataGrid.Columns[0]);