【问题标题】:How to access a control inside DataGrid.RowDetailsTemplate?如何访问 DataGrid.RowDetailsTemplate 中的控件?
【发布时间】:2013-07-11 19:11:10
【问题描述】:

以下是我的 XAML:

<DataGrid.RowDetailsTemplate>
    <DataTemplate>
        <DataGrid Name="innerGrid" IsReadOnly="True" AutoGenerateColumns="False" Margin="10,10,5,5" Width="400" HorizontalAlignment="Left">
            <DataGrid.Columns>
                <DataGridTextColumn  Header="Ref" Binding="{Binding Id}"/>
                <DataGridTextColumn  Header="Investor" Binding="{Binding FundProvider.FullName}"/>
                <DataGridTextColumn  Header="Amount" Binding="{Binding InvestmentAmount}"/>
            </DataGrid.Columns>
        </DataGrid>                                                 
    </DataTemplate>
</DataGrid.RowDetailsTemplate>

我的问题是如何从代码中访问innerGrid DataGrid 控件。提前致谢。

【问题讨论】:

  • 为什么要在代码中访问它?您的内部网格列是数据绑定的,因此绑定内部网格的ItemsSource 并在 ViewModel 中处理它
  • 实际上,当您单击节点时,我想将其设为树形下拉菜单。我想添加折叠和展开功能
  • 然后将你的内部网格放入Expander
  • 是的,可以做到!你能给我举个例子吗

标签: c# wpf xaml datagrid rowdetailstemplate


【解决方案1】:

您可以在LoadingRowDetails 事件(msdn) 中访问内部DataGrid

private void outerGrid_LoadingRowDetails(object sender, DataGridRowDetailsEventArgs e)
{
    DataGrid innerGrid = e.DetailsElement as DataGrid;
    if (innerGrid != null)
    {

    }
}

【讨论】:

    【解决方案2】:

    感谢 kmatyaszek 的正确答案。 以前我尝试使用 RowDetailsTemplate.LoadContent(),虽然能够获取对象,但它并没有更新 UI。

    这是我更新后的示例代码:

        void gridEmployee_LoadingRowDetails(object sender, DataGridRowDetailsEventArgs e)
        {
            Border border = e.DetailsElement as Border;
    
            if (border != null)
            {
                foreach (var grid in border.GetVisualChildren())
                {
                    Grid grid_ = grid as Grid;
    
                    if (grid_ != null)
                    {
                        foreach (var textBlock in grid_.GetVisualChildren())
                        {
                            TextBlock textBlock_ = textBlock as TextBlock;
    
                            if (textBlock_ != null && textBlock_.Text == "City : ")
                            {
                                textBlock_.Text = "My assigned text...";
                            }
                        }
                    }
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2016-03-29
      • 2020-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多