主/详细数据网格。如何访问Detail DataGrid来设置ItemsSource?

发布于 2024-12-02 14:12:21 字数 329 浏览 2 评论 0原文

在典型的主/细节情况下......

我有一个数据网格。此 DataGrid 的 ItemsSource 在 WCF 调用的 Completed 事件中设置 - (grdMaster.ItemsSource = e.Result) - 其中网格的 x:Name 是 grdMaster。这都是100%的。

但是,当在主网格 DataTemplate 中添加详细数据网格并对其进行适当命名时......我的代码隐藏无法识别详细网格。简单地说,我无法像设置 grdMaster 那样设置 grdDetail 的 ItemsSource。

根据所选的主项目,我需要执行 WCF 调用来获取适当的详细信息。

In a typical Master/Detail situation...

I have a DataGrid. The ItemsSource of this DataGrid is set 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红尘作伴 2024-12-09 14:12:21

根据如何通知您正在选择要扩展的项目,您将需要找到用户所在的行:

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

并更新行详细信息可见性:

row.DetailsVisibility = Visibility.Visible;

除了这些详细信息之外,您还需要为详细信息行创建一个样式 -它连接到一个您可以监听的事件:

<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>

它被设置为网格的 RowDetailsTemplate:

在 LoadingRow 事件中,您可以获得对所涉及的数据上下文的引用,并保存对子数据网格的引用,以便在 WCF 调用后您可以设置 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);
    }
}

希望有帮助,

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,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文