Access ScrollView properties of a DataGrid in WPF

后端 未结 2 1304
时光取名叫无心
时光取名叫无心 2021-01-13 12:58

Is it possible to access the horizontal offset, which I can\'t find in the property list of the datagrid ?

Thanks

相关标签:
2条回答
  • 2021-01-13 13:33

    Just name the base control (e.g. ListView) in XAML and access it by name in the code behind.

    0 讨论(0)
  • 2021-01-13 13:38

    In XAML

     <DataGrid Name="dataGrid1" ..... />
    

    If you want to access to the HorizontalOffset you need to access to the ScrollViewer contained inside the Datagrid

    one possible method to access the ScrollViewer is

    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dataGrid1); i++)
    {
           if (VisualTreeHelper.GetChild(dataGrid1, i) is ScrollViewer)
        {
                  ScrollViewer scroll =
            (ScrollViewer)(VisualTreeHelper.GetChild(dataGrid1, i));
                               Console.WriteLine(scroll.HorizontalOffset);
        }
    }
    

    Note that scroll.HorizontalOffset is read-only

    0 讨论(0)
提交回复
热议问题