none
my Windows crash with StackOverflowException on InitializeComponent() method WPF C# RRS feed

  • Question

  • hi ladies and guys

    I'm practising WPF (C#) and suddenly my forms (Window) started to throw an StackOverflowException on the InitializeComponent() method. All Windows including the main one. 

    Could anyone tell me why this occurs? If it is something wrong in the XAML and/or in the code behind ...

    Thank you in advance

    Pablo

    Tuesday, June 18, 2019 2:29 AM

Answers

  • The problem is

        <Window.DataContext>
            <local:MainWindow/>
        </Window.DataContext>

    because it create new MainWindow inside DataContext. If you want DataContext MainWindow and not new, you have two options:

    1. add inside constructor

    this.DataContext = this;

    2. or in xaml 

    <Window DataContext="{Binding RelativeSource={RelativeSource Self}}">
    </Window>

    here is full answer with similar or same problem >> LINK.

    • Marked as answer by Tigre Pablito Thursday, June 27, 2019 2:09 AM
    Wednesday, June 19, 2019 9:42 PM

All replies

  • hi ladies and guys

    I'm practising WPF (C#) and suddenly my forms (Window) started to throw an StackOverflowException on the InitializeComponent() method. All Windows including the main one. 

    Could anyone tell me why this occurs? If it is something wrong in the XAML and/or in the code behind ...

    Thank you in advance

    Pablo


    Hi   Tigre Pablito,  

    I'm not sure what operations you have done before the error occurred.

    I recommend that you can try to create a new window and test.

    Besides, you can give us a code snippets that anyone can use to reproduce your issue. This will help us quickly analyze your problem.

    Thank you for your understanding.


    Best regards

    Yong Lu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 18, 2019 3:32 AM
    Moderator
  • Hi Yong Lu

    Thanks for replying. I created the Window again and added all the code. When I added (the last one) this

        <Window.DataContext>
            <local:MainWindow/>
        </Window.DataContext>

    the StackOverflowException came back. Before the DataGrid didn't display but the app ran.

    Could you help me again?

    Thank you

    Regards

    this is my xaml code

    <Window x:Class="TelList.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:TelList"
            mc:Ignorable="d"
            Title="Contact List" Height="600" Width="800"
            Style="{StaticResource CustomWindowStyle}">
        
        <Window.DataContext>
            <local:MainWindow/>
        </Window.DataContext>
        
        <Grid Name="Container" Height="556" VerticalAlignment="Bottom">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="800"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="30"/>
                <RowDefinition Height="40"/>
                <RowDefinition Height="500"/>
            </Grid.RowDefinitions>
            <DockPanel Name="DP1" Grid.Row="0">
                <Menu DockPanel.Dock="Top" Height="30" FontSize="18" Grid.ColumnSpan="3">
                    <MenuItem Header="_File">
                        <MenuItem Header="_New" />
                        <MenuItem Header="_Edit" />
                        <MenuItem Header="_Remove" />
                        <Separator />
                        <MenuItem Header="_Print" />
                        <Separator />
                        <MenuItem Header="_Exit" />
                    </MenuItem>
                    <MenuItem Header="_Search">
                        <MenuItem Header="_By Name" />
                        <MenuItem Header="_By Phone" />
                        <MenuItem Header="_Various" />
                    </MenuItem>
                    <MenuItem Header="_Help">
                        <MenuItem Header="_About" />
                    </MenuItem>
                </Menu>
            </DockPanel>
            <DockPanel Name="DP2" Grid.Row="1">
                <ToolBarTray DockPanel.Dock="Top" Height="40" Width="800">
                    <ToolBar Height="40" Width="200">
                        <Button Height="36" Width="36" Click="OnButtonNewClicked">
                            <Image Source="C:\Users\Administrador\Documents\Visual Studio 2015\Projects\TelList\TelList\bin\Debug\new.png"/>
                        </Button>
                        <Button Height="36" Width="36" Click="OnButtonEditClicked">
                            <Image Source="C:\Users\Administrador\Documents\Visual Studio 2015\Projects\TelList\TelList\bin\Debug\edit.png"/>
                        </Button>
                        <Button  Height="36" Width="36" Click="OnButtonRemoveClicked">
                            <Image Source="C:\Users\Administrador\Documents\Visual Studio 2015\Projects\TelList\TelList\bin\Debug\remove.png"/>
                        </Button>
                        <Button Height="36" Width="36" Click="OnButtonResetClicked">
                            <Image Source="C:\Users\Administrador\Documents\Visual Studio 2015\Projects\TelList\TelList\bin\Debug\synchronize.png"/>
                        </Button>
                    </ToolBar>
                </ToolBarTray>
            </DockPanel>
            <DockPanel Name="DP3" Grid.Row="2">
                <DataGrid Name="TelGrid" ItemsSource="{ Binding View }" CanUserAddRows="False"
                          EnableRowVirtualization="False"
                          ColumnHeaderHeight="30" RowHeaderWidth="30" RowHeight="30" HeadersVisibility="All"
                          SelectionMode="Extended" AutoGenerateColumns="False">
                    <DataGrid.ColumnHeaderStyle>
                        <Style TargetType="DataGridColumnHeader">
                            <Setter Property="FontSize" Value="18"/>
                            <Setter Property="Foreground" Value="YellowGreen"/>
                            <Setter Property="Background" Value="Aquamarine"/>
                            <Setter Property="HorizontalContentAlignment" Value="Center"/>
                        </Style>
                    </DataGrid.ColumnHeaderStyle>
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Apellido" Binding="{Binding Surname}" Width="200" FontSize="16" IsReadOnly="True">
                        </DataGridTextColumn>
                        <DataGridTextColumn Header="Nombres" Binding="{Binding Name}" Width="200" FontSize="16" IsReadOnly="True">
                        </DataGridTextColumn>
                        <DataGridTemplateColumn Header="Teléfonos" Width="160" IsReadOnly="True">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <ComboBox Height="30" FontSize="16"
                                              ItemsSource="{ Binding Tels.PhoneNbrs }" 
                                              DisplayMemberPath="Number"                                           
                                              SelectedIndex="0" />
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                        <DataGridTextColumn Header="Dirección" Binding="{ Binding Address }" Width="160" FontSize="16" IsReadOnly="True">
                        </DataGridTextColumn>
                        <DataGridTextColumn Header="E-Mail" Binding="{ Binding Mail }" Width="160" FontSize="16" IsReadOnly="True">
                        </DataGridTextColumn>
                    </DataGrid.Columns>
                </DataGrid>
            </DockPanel>
        </Grid>
    </Window>
    
    and this is the xaml.cs (I have to perform it and make it shorter but it should work by itself, actually it did before)

    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Data;
    using System.Data.SqlClient;
    
    namespace TelList
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public List<Contacto> listaContactos;
            public List<Telefonos> listaTels;
    
            private ObservableCollection<Contact> MainList = new ObservableCollection<Contact>();
            private CollectionViewSource cvs = new CollectionViewSource();
            public ICollectionView View { get { return cvs.View; } }
    
            public MainWindow()
            {
                InitializeComponent();
    
                App.conexion = new SqlConnection();
                App.conexion.ConnectionString = @"Server = SuperPotro-PC\SQL2014EXPRESS; Database = OFFICE AGENDA; Trusted_Connection = True;";
    
                listaContactos = LogicaDeNegocio.CargarListaContactos(null, ref listaTels);
    
                RellenarListaContactos();
            }
    
            private void OnButtonNewClicked(object sender, RoutedEventArgs e)
            {
                //Window1 form = new Window1();
                //form.Owner = Application.Current.MainWindow;
                //form.Show();
            }
    
            private void OnButtonEditClicked(object sender, RoutedEventArgs e)
            {
    
            }
    
            private void OnButtonRemoveClicked(object sender, RoutedEventArgs e)
            {
    
            }
    
            private void OnButtonResetClicked(object sender, EventArgs e)
            {
    
            }
    
            private void RellenarListaContactos()
            {
                int a, b;
                string item;
                Contact obj;
    
                MainList.Clear();
    
                for (a = 0; a < listaContactos.Count; a++)
                {
                    obj = new Contact();
                    obj.Tels = new TelInfo();
                    obj.Tels.PhoneNbrs = new List<TelData>();
    
                    for (b = 0; b < listaTels[a].Tels.Count; b++)
                    {
                        if (listaTels[a].Tels[b].ContactoID > 0)
                        {
                            item = "";
                            item += listaTels[a].Tels[b].Titulo + ": ";
                            item += LogicaDeNegocio.TelConGuiones(listaTels[a].Tels[b].NroTel);
    
                            obj.Tels.PhoneNbrs.Add(new TelData());
                            obj.Tels.PhoneNbrs[b].ID = b + 1;
                            obj.Tels.PhoneNbrs[b].Number = item;
                            obj.Tels.ID = listaTels[a].Tels[b].ContactoID;
    
                            //**********************************************************++
                        }
                    }
    
                    obj.ID = listaContactos[a].Id;
                    obj.Surname = listaContactos[a].Apellido;
                    obj.Name = listaContactos[a].Nombre;
    
                    obj.Mail = listaContactos[a].CorreoE;
                    obj.Address = listaContactos[a].Direccion;
                    MainList.Add(obj);
                }
    
                cvs.Source = MainList;
            }
        }
    }
    

    Wednesday, June 19, 2019 9:02 PM
  • The problem is

        <Window.DataContext>
            <local:MainWindow/>
        </Window.DataContext>

    because it create new MainWindow inside DataContext. If you want DataContext MainWindow and not new, you have two options:

    1. add inside constructor

    this.DataContext = this;

    2. or in xaml 

    <Window DataContext="{Binding RelativeSource={RelativeSource Self}}">
    </Window>

    here is full answer with similar or same problem >> LINK.

    • Marked as answer by Tigre Pablito Thursday, June 27, 2019 2:09 AM
    Wednesday, June 19, 2019 9:42 PM