Row details view never gets disposed?

Mar 3, 2009 at 8:50 AM
I am trying to add settings preservation support to my custom DataGrid control (which inherits from the 3.5.31016.1 code). According to http://msdn.microsoft.com/en-us/library/6a0381s6.aspx under "Application Settings and Custom Controls", the correct place to call SaveComponentSettings() is from within Dispose(), so my version implements both IPersistComponentSettings and IDisposable. Here is a stripped down version of the XAML that I have:

    <tk:DataGrid
        SaveSettings="True" SettingsKey="MasterGridKey"
        RowDetailsVisibilityMode="VisibleWhenSelected"
        SelectionUnit="FullRow"
        AutoGenerateColumns="False"
        IsReadOnly="True"
        >
        <tk:DataGrid.Columns>
            <!-- ... -->
        </tk:DataGrid.Columns>
        <tk:DataGrid.RowDetailsTemplate>
            <DataTemplate>
                <tk:DataGrid
                    SaveSettings="True" SettingsKey="DetailsGridKey"
                    ItemsSource="{Binding Path=DetailsCollection}"
                    AutoGenerateColumns="False"
                    IsReadOnly="True"
                    >
                    <tk:DataGrid.Columns>
                        <!-- ... -->
                    </tk:DataGrid.Columns>
                </tk:DataGrid>
            </DataTemplate>
        </tk:DataGrid.RowDetailsTemplate>
    </tk:DataGrid>

The code to implement IDisposable is as follows:

        public void Dispose()
        {
            this.Dispose(true);

            GC.SuppressFinalize(this);
        }

        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    // Still safe to reference other objects

                    this.SaveComponentSettings();
                }
            }

            this.disposed = true;
        }

The outer grid is assigned to an ObservableCollection<> in the outer DataGrid's Loaded event handler.

However, Dispose() only ever gets called on the outer (master) grid, not the inner (details) view. I tried hooking up the UnloadingRowDetails event but that doesn't seem to fire either, although LoadingRowDetails fires when expected (whenever a new row details view becomes visible). Not even when I shut down the application does the inner grid's Dispose() method get called, which seems odd to me.

Am I missing something here? What is the recommended place to perform a SaveComponentSettings() for a row details view DataGrid? (I am primarily interested in capturing various properties from the members of the details view's Columns collection, so it needs to happen before the details DataGrid is actually unloaded.)