Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.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# 如何在wpf中自动滚动到datagrid的底部_C#_.net_Wpf_Datagrid - Fatal编程技术网

C# 如何在wpf中自动滚动到datagrid的底部

C# 如何在wpf中自动滚动到datagrid的底部,c#,.net,wpf,datagrid,C#,.net,Wpf,Datagrid,我有DataGrid,它有两种模式,ListView和alleryView。当我们在GalleryView中选择一个项目并切换到ListView时,同样的项目也会在列表视图中被选中。但scroll不会自动向下滚动到所选项目。如何让它自动工作 XAML文件 <DataGrid ItemsSource="{Binding ListItems}" RowStyle="{StaticResource DataGridRowStyle}" AutoGenerate

我有
DataGrid
,它有两种模式,
ListView
alleryView
。当我们在
GalleryView
中选择一个项目并切换到
ListView
时,同样的项目也会在列表视图中被选中。但scroll不会自动向下滚动到所选项目。如何让它自动工作

XAML文件

    <DataGrid ItemsSource="{Binding ListItems}" RowStyle="{StaticResource DataGridRowStyle}"
              AutoGenerateColumns="False" RowHeight="60" CanUserAddRows="False" 
              CanUserDeleteRows="False" CanUserResizeRows="False" AlternationCount="2"
              HorizontalGridLinesBrush="LightSteelBlue" VerticalGridLinesBrush="LightSteelBlue"
              SelectionMode="Single" SelectedItem="{Binding SelectedSearchItem}" 
              IsReadOnly="True" KeyboardNavigation.TabNavigation="Once" behaviors:DataGridBehavior.Autoscroll="{Binding Autoscroll}" >
行为文件:

     public static class DataGridBehavior
{
    public static readonly DependencyProperty AutoscrollProperty = DependencyProperty.RegisterAttached(
    "Autoscroll", typeof(bool), typeof(DataGridBehavior), new PropertyMetadata(default(bool), AutoscrollChangedCallback));

    private static readonly Dictionary<DataGrid, NotifyCollectionChangedEventHandler> handlersDict = new Dictionary<DataGrid, NotifyCollectionChangedEventHandler>();

    private static void AutoscrollChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
    {
        var dataGrid = dependencyObject as DataGrid;
        if (dataGrid == null)
        {
            throw new InvalidOperationException("Dependency object is not DataGrid.");
        }

        if ((bool)args.NewValue)
        {
            Subscribe(dataGrid);
            dataGrid.Unloaded += DataGridOnUnloaded;
            dataGrid.Loaded += DataGridOnLoaded;
        }
        else
        {
            Unsubscribe(dataGrid);
            dataGrid.Unloaded -= DataGridOnUnloaded;
            dataGrid.Loaded -= DataGridOnLoaded;
        }
    }

    private static void Subscribe(DataGrid dataGrid)
    {
        var handler = new NotifyCollectionChangedEventHandler((sender, eventArgs) => ScrollToEnd(dataGrid));
        handlersDict.Add(dataGrid, handler);
        ((INotifyCollectionChanged)dataGrid.Items).CollectionChanged += handler;
        ScrollToEnd(dataGrid);
    }

    private static void Unsubscribe(DataGrid dataGrid)
    {
        NotifyCollectionChangedEventHandler handler;
        handlersDict.TryGetValue(dataGrid, out handler);
        if (handler == null)
        {
            return;
        }
        ((INotifyCollectionChanged)dataGrid.Items).CollectionChanged -= handler;
        handlersDict.Remove(dataGrid);
    }

    private static void DataGridOnLoaded(object sender, RoutedEventArgs routedEventArgs)
    {
        var dataGrid = (DataGrid)sender;
        if (GetAutoscroll(dataGrid))
        {
            Subscribe(dataGrid);
        }
    }

    private static void DataGridOnUnloaded(object sender, RoutedEventArgs routedEventArgs)
    {
        var dataGrid = (DataGrid)sender;
        if (GetAutoscroll(dataGrid))
        {
            Unsubscribe(dataGrid);
        }
    }

    private static void ScrollToEnd(DataGrid datagrid)
    {
        if (datagrid.Items.Count == 0)
        {
            return;
        }
        datagrid.ScrollIntoView(datagrid.Items[datagrid.Items.Count - 1]);
    }

    public static void SetAutoscroll(DependencyObject element, bool value)
    {
        element.SetValue(AutoscrollProperty, value);
    }

    public static bool GetAutoscroll(DependencyObject element)
    {
        return (bool)element.GetValue(AutoscrollProperty);
    }
}

这些是我正在与之合作的项目。但这些变化并未得到反映。项目在切换时处于选中状态,但滚动条不会进入底部。当项目编号(即SelectedRecordIndex)大于10时,我希望它转到页面底部。要将
DataGrid
滚动到特定行(或列),您可以使用
DataGrid.ScrollIntoView
,它有两个重载:

滚动到视图(对象)
-滚动到datagrid视图中的指定项(即行)

滚动到视图(对象,DataGridColumn)
-滚动到指定的项和列

在你的情况下,你需要先过载。为了滚动到最后一行,您可以使用以下代码:

myDataGrid.ScrollIntoView(myDataGrid.Items[myDataGrid.Items.Count - 1]);

如果要在ViewModel中执行此操作,可以使用行为

您可以通过以下方式进行登记:


这里介绍了如何使用列表框,与DataGrid相同。

您可以使用
DataGrid.ScrollIntoView(DataGrid.SelectedItem)滚动到
SelectedItem
必须在哪个文件中提及此问题?在CS文件的SetSelectedEmonViewChange方法中不可能,因为它是一个ViewModel,无法在其中创建datagrid对象。我没有在后面编写任何代码。必须在后面编写代码。。有没有使用ViewModel的方法?因为我不想在xaml.cs文件中写任何代码隐藏的意思。这只是为了initialization@user3422209这是代码隐藏。如何将代码放在XAML中?而不是放在XAML中。。。在CS文件中。。。ViewModel.CS。。不是在Xaml中。cs@user3422209对不起,我不了解你。这是C代码,不是XAML。我不明白你哪里有问题。我有这些文件,XAML,XAML.CS和ViewModel.CS。。如问题所述。在xaml.cs中只调用constructor。。。。在Viewmodel.cs中,我们进行编码
    public partial class ItemGridControl : UserControl
{
    public ItemGridControl()
    {
        InitializeComponent();

    }

}
myDataGrid.ScrollIntoView(myDataGrid.Items[myDataGrid.Items.Count - 1]);