Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.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
Wpf Datagrid自动滚动以使最后一行可见_Wpf_Silverlight - Fatal编程技术网

Wpf Datagrid自动滚动以使最后一行可见

Wpf Datagrid自动滚动以使最后一行可见,wpf,silverlight,Wpf,Silverlight,如何使DataGrid始终保持最后一行可见? 与添加新项时自动滚动到底部一样是,您可以使用方法a将DataGrid项传递给此方法 XAML: <DataGrid x:Name="DataGrid" Grid.Row="1" Margin="5" AutoGenerateColumns="True" ItemsSource="{Binding Path=Users}"> private ObservableCollecti

如何使DataGrid始终保持最后一行可见? 与添加新项时自动滚动到底部一样

是,您可以使用方法a将DataGrid项传递给此方法

XAML:

<DataGrid x:Name="DataGrid" Grid.Row="1"
          Margin="5"
          AutoGenerateColumns="True"
          ItemsSource="{Binding Path=Users}">
 private ObservableCollection<User> _users;


    public ObservableCollection<User> Users
    {
        get
        {
            return _users;
        }

        set
        {
            _users = value;
            OnPropertyChanged("Users");
        }
    }
private void BtnAdd_OnClick(object sender, RoutedEventArgs e)
{
    Users.Add(new User { Id = Guid.NewGuid().ToString(), FirstName = "Bill", LastName = "Clinton" });

    //scroll to last added item
    DataGrid.ScrollIntoView(Users[Users.Count-1]);
}

这是一种使用LoadingRow事件的简单方法:

void dataGrid_LoadingRow(object sender, System.Windows.Controls.DataGridRowEventArgs e)
{
    dataGrid.ScrollIntoView(e.Row.Item);
}

只要记住在网格加载完成后禁用它。

在已经尝试了许多方法之后,这是最好的方法:

if (datagrid.Items.Count > 0)
            {
                var border = VisualTreeHelper.GetChild(datagrid, 0) as Decorator;
                if (border != null)
                {
                    var scroll = border.Child as ScrollViewer;
                    if (scroll != null) scroll.ScrollToEnd();
                }
            }
最简单的方法:

DataGrid.SelectedIndex = DataGrid.Items.Count - 1; // If you need to select the last item
DataGrid.ScrollIntoView(DataGrid.Items[DataGrid.Items.Count - 1]);

只有comment as没有在DataGrid中使用它,但是有一个BringIntoView方法,您可以在最后一行调用。是的,我知道我可以用不同的方式在代码中这样做,但我想知道是否有“自动跟踪”的设置。感谢您向我展示了如何向集合添加项目、引发属性更改和手动滚动。我想在网格上进行一些设置,以便在不编写像这样难看的代码的情况下自动执行此操作。@Magnus Ahlin:如果您使用例如MVVM,您可以创建自己的DataGrid并在内部调用此方法。我认为WPF中的DataGrid没有用于此目的的属性。您可以将此代码包装成一个在每次添加项时都会执行此操作的文件。“请记住在网格加载完成后禁用它”,这是如何完成的?