wpf datagrid autoscroll

I would like to set up a datagrid so that whenever an item is added to its itemssource the datagrid scrolls down to show the last item.

The datagrid is inside a datatemplate, so i cannot set the X:name property and access it directly from the codebehind.

What I have in mind is to use a datagrid event that fires when a row is added and has the grid scroll itself.

Here's some psuedo code that outlines how I have things set up:

UI.XAML exerpt
<TabControl ItemsSource="{Binding Parents}" x:Name="ProductsTab">
            <TextBlock Text="{Binding Key}"/>
            <DataGrid Margin="5" ItemsSource="{Binding Value.Children}">
                    <Column Column definitions removed for your sanity/>

UI.XAML.CS exerpt
public class UI
    //Thanks to Dr. WPF for the ObservableDictionary class
    public ObservableDictionary<string, Parent> Parents {get; set;}

public class parent
    public ObservableCollection<Child> Children {get; set;}

The datagrids are not editable.

In case you're wondering, I have read the post "How to autoscroll on WPF datagrid" the code in that post would work for me if I could find an event that fires whenever an item is added to the datagrid itemssource.

Any Ideas?


Combine the autoscrolling idea with the idea from this question or this MSDN thread: instead of listening to your grid's event to detect row additions, listen to the events from the ItemsSource.

Edit: Since you don't like that suggestion, you could try hooking LoadingRow, but I strongly suspect this will require EnableRowVirtualization = false in order to work (I haven't tried it). If your collection gets large, turning off row virutalization opens up the possibility of a serious performance hit.

you can access the DataGrid, even if it is in a DataTemplate, by doing a 'search' in the visual tree : VisualTreeHelper.GetChildCount // VisualTreeHelper.GetChild , then test again the type until you find your grid. And you might seek with same kind of methods the ScrollBar, and then you can hook an event handler and use a code-behind logic.

Need Your Help

How do I preserve Polymorphism when I use C++/CLI to wrap a C++ API?

visual-c++ c++-cli

I have a large C++ API, which I need to wrap in a C++/CLI skin to make available in .NET

delete existing row from data table

asp.net gridview

I need to delete a row from datatable according to the selected value from the grid view

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.