private void GenerationChangedHandler(object sender, GenerationChangedEventArgs e)
        {
            if (e.duplicatedName != string.Empty)
            {
                foreach (Generation generation in generationList)
                {
                    foreach (Grid grid in generation.GenerationGridList)
                    {
                        IEnumerable <TextBox> textBoxList = grid.Children.OfType <TextBox>().Where(t => t.Text == e.duplicatedName);
                        foreach (TextBox textBox in textBoxList)
                        {
                            textBox.Foreground = Brushes.Red;
                            foreach (Label label in grid.Children.OfType <Label>().Where(l => Grid.GetColumn(l) == Grid.GetColumn(textBox)))
                            {
                                label.Foreground = Brushes.Red;
                            }
                            foreach (TextBox textbox in grid.Children.OfType <TextBox>().Where(t => Grid.GetColumn(t) == Grid.GetColumn(textBox)))
                            {
                                textbox.Foreground = Brushes.Red;
                            }
                        }
                    }
                }
            }

            GenerationChanged?.Invoke(sender, e);
        }
        private void ConnectChildrenToParents(object sender, GenerationChangedEventArgs e)
        {
            Generation            generation = (Generation)sender;
            List <Node <Person> > personList = new List <Node <Person> >();

            foreach (Grid grid in generation.GenerationGridList)
            {
                foreach (TextBox textBox in grid.Children.OfType <TextBox>())
                {
                    Node <Person> nextPerson = null;
                    if (generation.TextBoxList.IndexOf(textBox) != generation.TextBoxList.Count - 1)
                    {
                        if (PersonTree.GetNodeByName(PersonTree.Tree, textBox.Text,
                                                     generation.TextBoxList[generation.TextBoxList.IndexOf(textBox) + 1].Text) != null)
                        {
                            nextPerson = PersonTree.GetNodeByName(PersonTree.Tree, textBox.Text,
                                                                  generation.TextBoxList[generation.TextBoxList.IndexOf(textBox) + 1].Text);
                        }
                    }

                    else
                    {
                        nextPerson = PersonTree.GetNodeByName(PersonTree.Tree, textBox.Text, string.Empty);
                    }
                    if (nextPerson != null)
                    {
                        if (nextPerson.Value.GenerationID == generation.GenerationID)
                        {
                            if (!personList.Any(item => item.Value.Name == nextPerson.Value.Name))
                            {
                                personList.Add(nextPerson);
                            }
                        }
                    }
                }
            }

            for (int i = 0; i < ConnectionsGrid.Children.Count; i++)
            {
                if (ConnectionsGrid.Children[i].GetType() == typeof(Line) &&
                    ((Line)ConnectionsGrid.Children[i]).Name == "Child" + generation.GenerationID.ToString().Replace("-", string.Empty))
                {
                    ConnectionsGrid.Children.Remove(ConnectionsGrid.Children[i]);
                    i--;
                }
            }

            //for (int i = 0; i < generation.ParentsGridList[parentIndex].Children.Count - 1; i++)
            //{
            //    if (generation.ParentsGridList[parentIndex].Children[i].GetType() == typeof(Line) &&
            //        ((Line)generation.ParentsGridList[parentIndex].Children[i]).Name ==
            //        "Child" + generation.GenerationID.ToString().Replace("-", string.Empty))
            //    {
            //        generation.ParentsGridList[parentIndex].Children.Remove(generation.ParentsGridList[parentIndex].Children[i]);
            //        i--;
            //    }
            //}

            foreach (Node <Person> person in personList)
            {
                int  parentIndex       = 0;
                int  parentColumnIndex = 0;
                bool found             = false;
                for (int i = 0; i < generation.ParentsGridList.Count; i++)
                {
                    for (int j = 0; j < generation.ParentsGridList[i].Children.Count && !found; j++)
                    {
                        if (generation.ParentsGridList[i].Children[j].GetType() == typeof(TextBox))
                        {
                            if ((string)generation.ParentsGridList[i].Children[j].GetValue(TextBox.TextProperty) == person.Parent.Value.Name)
                            {
                                parentIndex       = i;
                                parentColumnIndex = Grid.GetColumn(generation.ParentsGridList[i].Children[j]);
                                found             = true;
                            }
                        }
                    }
                }

                Line verticalLine1 = new Line()
                {
                    Stroke          = Brushes.Black,
                    Visibility      = Visibility.Visible,
                    StrokeThickness = 1,
                    X1      = 12.5,
                    X2      = 12.5,
                    Y1      = SystemFonts.MessageFontSize,
                    Y2      = SystemFonts.MessageFontSize * 2 + 1,
                    Stretch = Stretch.None,
                    Name    = "Child" + generation.GenerationID.ToString().Replace("-", string.Empty)
                };
                generation.ParentsGridList[parentIndex].Children.Add(verticalLine1);
                Grid.SetRow(verticalLine1, 0);
                Grid.SetColumn(verticalLine1, parentColumnIndex + 1);

                Line verticalLine2 = new Line()
                {
                    Stroke          = Brushes.Black,
                    Visibility      = Visibility.Visible,
                    StrokeThickness = 1,
                    X1      = 12.5,
                    X2      = 12.5,
                    Y1      = 0,
                    Y2      = SystemFonts.MessageFontSize * 2 + 2,
                    Stretch = Stretch.None,
                    Name    = "Child" + generation.GenerationID.ToString().Replace("-", string.Empty)
                };
                generation.ParentsGridList[parentIndex].Children.Add(verticalLine2);
                Grid.SetRow(verticalLine2, 1);
                Grid.SetColumn(verticalLine2, parentColumnIndex + 1);

                Line verticalLine3 = new Line()
                {
                    Stroke          = Brushes.Black,
                    Visibility      = Visibility.Visible,
                    StrokeThickness = 1,
                    X1      = 12.5,
                    X2      = 12.5,
                    Y1      = 0,
                    Y2      = SystemFonts.MessageFontSize * 3 + 8,
                    Stretch = Stretch.None,
                    Name    = "Child" + generation.GenerationID.ToString().Replace("-", string.Empty)
                };
                generation.ParentsGridList[parentIndex].Children.Add(verticalLine3);
                Grid.SetRow(verticalLine3, 2);
                Grid.SetColumn(verticalLine3, parentColumnIndex + 1);

                Line verticalLine4 = new Line()
                {
                    Stroke          = Brushes.Black,
                    Visibility      = Visibility.Visible,
                    StrokeThickness = 1,
                    X1      = 12.5,
                    X2      = 12.5,
                    Y1      = 0,
                    Y2      = SystemFonts.MessageFontSize * 3 + 8,
                    Stretch = Stretch.None,
                    Name    = "Child" + generation.GenerationID.ToString().Replace("-", string.Empty)
                };
                generation.ParentsGridList[parentIndex].Children.Add(verticalLine4);
                Grid.SetRow(verticalLine4, 4);
                Grid.SetColumn(verticalLine4, parentColumnIndex + 1);

                verticalLine4.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
                verticalLine4.Arrange(new Rect(verticalLine3.DesiredSize));
                verticalLine4.UpdateLayout();

                Point endPoint = verticalLine4.TranslatePoint(new Point(verticalLine4.X2, verticalLine4.Y2), ConnectionsGrid);

                int generationGridIndex = 0;
                int textBoxColumnIndex  = 0;
                foreach (Grid grid in generation.GenerationGridList)
                {
                    foreach (TextBox textBox in grid.Children.OfType <TextBox>())
                    {
                        if (textBox.Text == person.Value.Name)
                        {
                            generationGridIndex = generation.GenerationGridList.IndexOf(grid);
                            textBoxColumnIndex  = Grid.GetColumn(textBox);
                        }
                    }
                }

                Line verticalLine5 = new Line()
                {
                    Stroke          = Brushes.Black,
                    Visibility      = Visibility.Visible,
                    StrokeThickness = 1,
                    X1      = 125,
                    X2      = 125,
                    Y1      = 4,
                    Y2      = -SystemFonts.MessageFontSize * 2 - 2,
                    Stretch = Stretch.None,
                    Name    = "Child" + generation.GenerationID.ToString().Replace("-", string.Empty)
                };
                generation.GenerationGridList[generationGridIndex].Children.Add(verticalLine5);
                Grid.SetRow(verticalLine5, 0);
                Grid.SetColumn(verticalLine5, textBoxColumnIndex);

                verticalLine5.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
                verticalLine5.Arrange(new Rect(verticalLine5.DesiredSize));
                verticalLine5.UpdateLayout();

                Point startPoint = verticalLine5.TranslatePoint(new Point(verticalLine5.X2, verticalLine5.Y2), ConnectionsGrid);

                Line horizontalLine1 = new Line()
                {
                    Stroke          = Brushes.Black,
                    Visibility      = Visibility.Visible,
                    StrokeThickness = 1,
                    X1      = startPoint.X,
                    X2      = endPoint.X,
                    Y1      = startPoint.Y,
                    Y2      = endPoint.Y,
                    Stretch = Stretch.None,
                    Name    = "Child" + generation.GenerationID.ToString().Replace("-", string.Empty)
                };
                ConnectionsGrid.Children.Add(horizontalLine1);

                if (person.Value.Partner != string.Empty)
                {
                    Line horizontalLine = new Line()
                    {
                        Stroke          = Brushes.Black,
                        Visibility      = Visibility.Visible,
                        StrokeThickness = 1,
                        X1      = 0,
                        X2      = 25,
                        Y1      = SystemFonts.MessageFontSize,
                        Y2      = SystemFonts.MessageFontSize,
                        Stretch = Stretch.None,
                        Name    = "Child" + generation.GenerationID.ToString().Replace("-", string.Empty)
                    };
                    generation.GenerationGridList[generationGridIndex].Children.Add(horizontalLine);
                    Grid.SetRow(horizontalLine, 0);
                    Grid.SetColumn(horizontalLine, textBoxColumnIndex + 1);
                }
            }

            List <Node <Person> > parentsList = new List <Node <Person> >();

            foreach (Node <Person> person in personList)
            {
                if (!parentsList.Contains(person.Parent))
                {
                    parentsList.Add(person.Parent);
                }
            }

            foreach (Node <Person> parent in parentsList)
            {
                Node <Person> firstPerson = personList.FirstOrDefault(p => p.Parent == parent);
                Node <Person> lastPerson  = personList.LastOrDefault(p => p.Parent == parent);

                Grid generationGrid = null;

                foreach (Grid grid in generation.GenerationGridList)
                {
                    foreach (TextBox textBox in grid.Children.OfType <TextBox>())
                    {
                        if (textBox.Text == firstPerson.Value.Name)
                        {
                            generationGrid = grid;
                        }
                    }
                }

                int numberOfPartners = 0;
                for (int i = personList.IndexOf(firstPerson); i < personList.IndexOf(lastPerson); i++)
                {
                    if (personList[i].Value.Partner != string.Empty)
                    {
                        numberOfPartners++;
                    }
                }
            }
        }