开发者

Master/Detail DataGrid. How to access Detail DataGrid to set ItemsSource?

In a typical Master/Detail situation...

I have a DataGrid. The ItemsSource of this DataGrid is set开发者_StackOverflow社区 in the Completed event of a WCF call - (grdMaster.ItemsSource = e.Result) - where the x:Name of the grid is grdMaster. This is all 100%.

However, when adding a Detail Datagrid inside the master grids DataTemplate and naming it appropriately... my codebehind does not recognise the detail grid. So plainly put, I cannot set the ItemsSource of grdDetail like I do with grdMaster.

Depending on the Master item selected, I need to do a WCF call to get the appropriate Details.


Depending on how you are being notified that an item is being selected for expansion you will need to find the row the user is in:

DataGridRow row = DataGridRow.GetRowContainingElement(...);

and update the row details visibility:

row.DetailsVisibility = Visibility.Visible;

Those details aside, you need to create a style for the details rows -- which is wired to an event you can listen to:

<DataTemplate x:Key="DetailsRowTemplate">
    <StackPanel>
        <Border BorderBrush="{StaticResource BlackBrush}" BorderThickness="0,2,0,0" Padding="0" >
            <data:DataGrid ItemsSource="{Binding DummyResultsView}" AutoGenerateColumns="False"
                            LoadingRow="DataGrid_LoadingRow" 
                            CanUserResizeColumns="False" 
                            CanUserReorderColumns="False"
                            HeadersVisibility="None"
                            IsReadOnly="True">
            </data:DataGrid>
        </Border>
    </StackPanel>
</DataTemplate>

which is set as the RowDetailsTemplate for your grid:

Within the LoadingRow event you can obtain a reference to which data context is involved, and save a reference to the child data grid so that after a WCF call you can set the ItemsSource:

private void DataGrid_LoadingRowDetails(object sender, DataGridRowDetailsEventArgs e)
{
    List<DataGrid> detailElements = e.DetailsElement.GetChildrenByType<System.Windows.Controls.DataGrid>().ToList();

    var itemSelected = e.Row.DataContext;

    if (detailElements.Count > 0)
    {
        DataGrid detailsDataGrid = detailElements[0];

        // save a reference so the ItemsSource can be set later....
        this.DataGrid = detailsDataGrid;

        this.Model.InitializeDetailsList(detailsDataGrid, itemSelected);
    }
}

Hope that helps,

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜