views:

342

answers:

0

Hello,

I have a DataGrid with about 4000 rows. Scrolling through the DataGrid works well, until I add a RowDetailsTemplate (which is a UserControl that I created). My guess is that my UserControl is being created as the DataGridRows come into view, causing incredibly slow scrolling.

Does anyone know of a way to prevent any initialization of the RowDetailsTemplate until the row is selected? If that's not possible, is there any way to make my DataTemplate lazy-load all elements below the root visual?

If I can make either of those happen, I should be able to dramatically increase the performance of my application.

Thanks,
-Charles

[Edit - 08/04/09]
Alright - I came up with a really lousy hack that gets the performance I need, but results in the previously selected row staying open for an indeterminate amount of time:

    private void DataGrid_RowDetailsVisibilityChanged(object sender, DataGridRowDetailsEventArgs e)
    {
        var row = e.Row;
        if (lastSelectedRow != row)
        {
            if (lastSelectedRow != null)
            {
                // Remove the data template from the last selected row
                lastSelectedRow.ClearValue(DataGridRow.DetailsTemplateProperty);
            }

            // Set the data template on the new row
            var detailsTemplate = (DataTemplate)this.Resources["reportItemDetailsTemplate"];
            row.DetailsTemplate = detailsTemplate;

            this.Dispatcher.BeginInvoke(() => row.UpdateLayout());

            lastSelectedRow = row;
        }
    }

The RowDetailsVisibilityChanged event would only fire if I set the DataGrid.RowDetailsTemplate property to something, so this is what I used as a place holder:

            <data:DataGrid.RowDetailsTemplate>
                <DataTemplate>
                    <!-- Use a Canvas as a light-weight placeholder -->
                    <Canvas />
                    <!--<local:ReportItemDetails />-->
                </DataTemplate>
            </data:DataGrid.RowDetailsTemplate>


The only problem now is that the previously selected row closes in a very annoying, unpredictable way.

If your reading this thinking "Dude, why are you making it so hard - just do xyz!" - PLEASE SHARE :). If not, any suggestions on improving this hack would be much appreciated.