Row details view DataGrid is read-write but set as read-only? - never mind

Feb 2, 2009 at 8:50 AM
I have a scenario in which it makes sense to nest two DataGrid controls to display some information. The "outer" object is a collection of instances of a class with two fields, let's call them Name and Data. "Name" is a string, and "Data" is an ObservableCollection<InnerClass>. InnerClass, in turn, has a few fields of various types. I am using a set of controls derived from the V1 DataGrid controls but which add certain functionality relevant to this project, however I don't see that being an issue in this case.

What I want is for the Data to be displayed in a RowDetailsTemplate containing a second DataGrid. I have managed to get the information on the screen under the desired conditions, but even though I disable (as far as I can tell) all editing properties on the details template DataGrid, it still allows the user to edit the contents of the Data collection. In this situation that is a serious drawback. The outer DataGrid, as far as I can tell, has all relevant properties set to the same values, and that one is read-only.

As it is, I am populating an ObservableCollection<> of the outer object type through static code and then assigning that collection to the outer datagrid's ItemsSource property. Here is the XAML, could someone please take a look at it and point me in the right direction?

Many thanks in advance.


<Page x:Class="MyNamespace.MyPageClass"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:tk="clr-namespace:MyNamespace.MyDerivedWpfToolkit"
    Title="Main">
    <tk:DataGrid
        RowDetailsVisibilityMode="VisibleWhenSelected"
        SelectionUnit="FullRow"
        Loaded="DataGrid_Loaded"
        Name="dataGrid"
        AutoGenerateColumns="False"
        CanUserAddRows="False"
        CanUserDeleteRows="False"
        CanUserEditMultipleRows="False"
        IsReadOnly="True"
        >
        <tk:DataGrid.Columns>
            <tk:DataGridTextColumn Binding="{Binding Path=Name}" />
        </tk:DataGrid.Columns>
        <tk:DataGrid.RowDetailsTemplate>
            <DataTemplate>
                <tk:DataGrid
                    ItemsSource="{Binding Path=Data}"
                    AutoGenerateColumns="False"
                    CanUserAddRows="False"
                    CanUserDeleteRows="False"
                    CanUserEditMultipleRows="False"
                    IsReadOnly="True"
                    >
                    <tk:DataGrid.Columns>
                        <tk:DataGridTextColumn Binding="{Binding Path=Field1}" />
                        <tk:DataGridTextColumn Binding="{Binding Path=Field2}" />
                    </tk:DataGrid.Columns>
                </tk:DataGrid>
            </DataTemplate>
        </tk:DataGrid.RowDetailsTemplate>
    </tk:DataGrid>
</Page>
Feb 2, 2009 at 9:35 AM
Setting IsReadOnly="True" on the RowDetailsTemplate DataGrid makes the data read only, but the empty row (normally used for adding new data) remains. Why is it there and what can I do to remove it?

I suppose in the end what my question boils down to is, why does the DataGrid inside the RowDetailsTemplate act differently than the top-level DataGrid?
Feb 2, 2009 at 9:47 AM
Never mind - I found a CanUserAddRows assignment in my derived class' constructor that wasn't supposed to be there. My sincere apologies to anyone who spent time on this; it was clearly programmer error.