DataGrid AutoGeneratingColumn event not fired for DataTemplateSelector?

in my WPF there is a DataGrid which columns are auto-generated. The data is loaded from extern, so the column names are known at runtime. For certain columns, i display a button inside the column. Only when clicked onto the button, i load other data and display it inside the RowDetails of the outer DataGrid. Then, the inner DataGrid is loaded again – maybe with other buttons. This is repeated for ever.

I want to do this with a DataTemplateSelector. This is my current .xaml code:

<Window.Resources>
    <local:DynamicDataTemplateSelector x:Key="dynamicTemplateSelector" />
</Window.Resources>
<DataGrid ItemsSource="{Binding DataTable1.DefaultView}" AutoGenerateColumns="True"
                        AutoGeneratingColumn="DataGrid_AutoGeneratingColumn" CanUserAddRows="False>
    <DataGrid.RowDetailsVisibilityMode>Collapsed</DataGrid.RowDetailsVisibilityMode>
    <DataGrid.RowDetailsTemplateSelector>
        <StaticResource ResourceKey="dynamicTemplateSelector"/>
    </DataGrid.RowDetailsTemplateSelector>
</DataGrid>

DataGrid_AutoGeneratingColumn(sender, e) calls HandlerDgAutoGeneratingColumn(sender, e) In this method i declare an event when the button inside the cell is clicked. DataTable1 is a DataTable with geter/seter, i added the RaisePropertyChanged Event for MVVM.

In my DataTemplateSelector i build a new DataGrid if there is one column that contains a button. The inner DataGrid is fully loaded and correctly bound. I build the DataGrid visually in the correct style. But: The AutoGeneratedEvent is never fired. The breakpoints in DataGrid_AutoGeneratingColumnTempl are never reached. I cannot figure out, why.

This is my current class DataTemplateSelector:

public class DynamicDataTemplateSelector : DataTemplateSelector
  {
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
      if (item is DataRowView dataRowView)
      {
        foreach (var column in dataRowView.DataView.Table.Columns.Cast<DataColumn>())
        {
          if (column.DataType == typeof(DataTable))
          {
            var template = new DataTemplate();
            var dataGridFactory = new FrameworkElementFactory(typeof(DataGrid));

            // Binding ItemsSource to the inner DataTable's DefaultView
            dataGridFactory.SetBinding(DataGrid.ItemsSourceProperty, new Binding($"{column.ColumnName}.DefaultView"));
            dataGridFactory.SetValue(DataGrid.AutoGenerateColumnsProperty, true);

            // Setting CanUserAddRows to False
            dataGridFactory.SetValue(DataGrid.CanUserAddRowsProperty, false);

            // Setting RowDetailsVisibilityMode to Collapsed
            dataGridFactory.SetValue(DataGrid.RowDetailsVisibilityModeProperty, DataGridRowDetailsVisibilityMode.Collapsed);

            // Adding the AutoGeneratingColumn event
            dataGridFactory.AddHandler(DataGrid.LoadedEvent, new RoutedEventHandler((sender, args) =>
            {
              var dataGrid = sender as DataGrid;
              if (dataGrid != null)
              {
                // This code is executed
                dataGrid.AutoGeneratingColumn += DataGrid_AutoGeneratingColumnTempl;
              }
            }));

            // Setting up RowDetailsTemplateSelector for recursive templates
            dataGridFactory.SetValue(DataGrid.RowDetailsTemplateSelectorProperty,
                new DynamicResourceExtension("dynamicTemplateSelector"));


            template.VisualTree = dataGridFactory;
            return template;
          }
        }
      }
      return base.SelectTemplate(item, container);
    }

    private void DataGrid_AutoGeneratingColumnTempl(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {
      //This code is never reached.

      // Handle the AutoGeneratingColumn event to customize columns
      MainWindow mainWindow = Application.Current.MainWindow as MainWindow;
      if (mainWindow != null)
      {
        mainWindow.HandlerDgAutoGeneratingColumn(sender, e);  
      }
    }
  }

I tried asking this question to ChatGPT with no success. I expect DataGrid_AutoGeneratingColumnTempl to be called. I should be able to see 3 inner DataGrids. But i only see one where the buttons in the third columns are not displayed: Status Quo. There should be buttons in the “InnerDataTable” column.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật