'if itemcontainerstyle is inside DataTemplate and a call the the main window is used an System.NullReferenceExc on start-up but works fine if ignored

when i try to call a event from the main wind if a control is in a template in itemcontainerstyle when the run a System.NullReferenceException: 'Object reference not set to an instance of an object.' is displayed over InitializeComponent(); which if i continue it works fine as indented which is weird because an error message is normally there to tell you if something didn't work

below is the a cut down vision of my program only the things that case the error message to come up (i have tried it in different templates)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace wpf_test_zone
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void test(object sender, RoutedEventArgs e)
        {

        }
    }

       Title="MainWindow" Height="450" Width="800">
    <Grid>
        <TabControl x:Name="tabcontrol_1">
            <TabControl.ContentTemplate>
                <DataTemplate>
                   
                </DataTemplate>
            </TabControl.ContentTemplate>
        </TabControl>
        <ListView>
            <ListView.ItemTemplate>
                <DataTemplate>
                     <Grid>
                        <TabControl x:Name="tabcontrol_2">
                            <TabControl.ItemContainerStyle>
                                
                                <Style TargetType="TabItem">
                                    <Setter Property="Header" Value="{Binding}"/>
                                    <Setter Property="HeaderTemplate">
                                        <Setter.Value>
                                            <DataTemplate>
                                                <StackPanel>
                                                    <Grid>
                                                        <Button x:Name="but" Click="test">
                                                        </Button>
                                                    </Grid>
                                                </StackPanel>
                                            </DataTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </TabControl.ItemContainerStyle>
                        </TabControl>
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        
        
    </Grid>

edit -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= the error message that comes up



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source