/// <summary>
        /// Wyszukujemy wszyskie pliki których nazwy zawieraja podany przez
        /// nas ciag znakow w textboxie
        /// </summary>

        private void button12_Click(object sender, RoutedEventArgs e)
        {
            MyDirectory myDirectory1 = new MyDirectory(directoryPath.Text);

            listOfUserControls1.Children.Clear();

            List <DiscElement> subElements1 = myDirectory1.GetDiscElements();
            List <DiscElement> results      = subElements1.FindAll(x => x.Name.Contains(textBox.Text));

            foreach (DiscElement discElement in results)
            {
                {
                    ElementsViews discElementViews = new ElementsViews(discElement);

                    listOfUserControls1.Children.Add(discElementViews);
                    discElementViews.checkedChangeEv += CheckedChangeEv;
                }
            }
            //------------------------------------------------------------
            MyDirectory myDirectory2 = new MyDirectory(directoryPath2.Text);

            listOfUserControls2.Children.Clear();

            List <DiscElement> subElements2 = myDirectory2.GetDiscElements();
            List <DiscElement> results2     = subElements2.FindAll(x => x.Name.Contains(textBox.Text));

            foreach (DiscElement discElement in results2)
            {
                ElementsViews discElementViews = new ElementsViews(discElement);

                listOfUserControls2.Children.Add(discElementViews);
                discElementViews.checkedChangeEv += CheckedChangeEv;
            }
        }
 /// <summary>
 /// Metoda aktywowana przez event CheckedChangeEv, która zmienia kolor zaznaczonego UserControl
 /// </summary>
 private void CheckedChangeEv()
 {
     foreach (object i in listOfUserControls1.Children)
     {
         ElementsViews elementViews = (ElementsViews)i;
         if (elementViews.checkBox.IsChecked.Value)
         {
             elementViews.Background = new SolidColorBrush(Color.FromRgb(116, 166, 247));
         }
         else
         {
             elementViews.Background = new SolidColorBrush(Color.FromRgb(255, 255, 255));
         }
     }
     foreach (object i in listOfUserControls2.Children)
     {
         ElementsViews elementViews = (ElementsViews)i;
         if (elementViews.checkBox.IsChecked.Value)
         {
             elementViews.Background = new SolidColorBrush(Color.FromRgb(116, 166, 247));
         }
         else
         {
             elementViews.Background = new SolidColorBrush(Color.FromRgb(255, 255, 255));
         }
     }
 }
        /// <summary>
        /// Metoda generuje naszą listę podfolderów i plików w folderze dla 2 stack panelu
        /// </summary>
        private void GenerateFilesList2()
        {
            MyDirectory myDirectory2 = new MyDirectory(directoryPath2.Text);

            listOfUserControls2.Children.Clear();

            List <DiscElement> subElements2 = myDirectory2.GetDiscElements();

            foreach (DiscElement discElement in subElements2)
            {
                ElementsViews discElementViews = new ElementsViews(discElement);

                listOfUserControls2.Children.Add(discElementViews);
                discElementViews.checkedChangeEv += CheckedChangeEv;
            }
        }
        /// <summary>
        /// Jesli zaznaczony element jest plikiem kopiujemy go do folderu po lewej stronie
        /// Jesli jest folderem to tworzymy nowy folder i kopiujemy wszystkie jego pliki
        /// </summary>

        private void button7_Click(object sender, RoutedEventArgs e)
        {
            foreach (object k in listOfUserControls2.Children)
            {
                ElementsViews elementViews = (ElementsViews)k;
                if (elementViews.checkBox.IsChecked.Value)
                {
                    if (elementViews.discElement is MyFile)
                    {
                        try
                        {
                            string fName = elementViews.discElement.Name;
                            string path  = elementViews.discElement.Path;
                            File.Copy(path, System.IO.Path.Combine(directoryPath.Text, fName));
                        }
                        catch (Exception)
                        {
                            MessageBox.Show("nie można skopiować pliku");
                        }
                    }
                    //-----------------------------
                    else
                    {
                        try
                        {
                            string dirName = elementViews.discElement.Name;
                            string pathx   = System.IO.Path.Combine(directoryPath.Text, dirName);
                            Directory.CreateDirectory(pathx);
                            MyDirectory   myDirectory1 = new MyDirectory(elementViews.discElement.Path);
                            List <MyFile> subElements  = myDirectory1.GetAllFiles();
                            foreach (MyFile file in subElements)
                            {
                                string fName = file.Name;
                                File.Copy(file.Path, System.IO.Path.Combine(pathx, fName));
                            }
                        }
                        catch (Exception)
                        {
                            MessageBox.Show("Nie można skopiować folderu");
                        }
                    }
                }
            }
            GenerateFilesList1();
        }
        //----------------------------------------------
        private void Open_new_folder2(string path)
        {
            directoryPath2.Text = path;
            MyDirectory myDirectory2 = new MyDirectory(directoryPath2.Text);

            listOfUserControls2.Children.Clear();


            List <DiscElement> subElements2 = myDirectory2.GetDiscElements();

            foreach (DiscElement discElement in subElements2)
            {
                ElementsViews discElementViews = new ElementsViews(discElement);

                listOfUserControls2.Children.Add(discElementViews);
                discElementViews.checkedChangeEv += CheckedChangeEv;
            }
        }
        /// <summary>
        /// usuwamy zaznaczone pliki metodą File.Delete (Nie usuwa folderow)
        /// </summary>


        private void button1_Click(object sender, RoutedEventArgs e)
        {
            foreach (object pi in listOfUserControls1.Children)
            {
                ElementsViews elementViews = (ElementsViews)pi;
                if (elementViews.checkBox.IsChecked.Value)
                {
                    try
                    {
                        System.IO.File.Delete(elementViews.discElement.Path);
                    }
                    catch (UnauthorizedAccessException)
                    {
                        MessageBox.Show("Brak dostępu do pliku");
                    }
                    catch (System.IO.IOException)
                    {
                        MessageBox.Show("Plik w użyciu");
                    }
                }
            }
            //----------------------------------------------------
            foreach (object pi in listOfUserControls2.Children)
            {
                ElementsViews elementViews = (ElementsViews)pi;
                if (elementViews.checkBox.IsChecked.Value)
                {
                    try
                    {
                        System.IO.File.Delete(elementViews.discElement.Path);
                    }
                    catch (UnauthorizedAccessException)
                    {
                        MessageBox.Show("Brak dostępu do pliku");
                    }
                    catch (System.IO.IOException)
                    {
                        MessageBox.Show("Plik w użyciu");
                    }
                }
            }
            GenerateFilesList1();
            GenerateFilesList2();
        }
        //-------------------------------------------------

        private void button11_Click(object sender, RoutedEventArgs e)
        {
            MyDirectory myDirectory2 = new MyDirectory(directoryPath2.Text);

            listOfUserControls2.Children.Clear();

            List <DiscElement> subElements2 = myDirectory2.GetDiscElements();

            subElements2.Sort((x, y) => DateTime.Compare(x.CreationTime, y.CreationTime));

            foreach (DiscElement discElement in subElements2)
            {
                ElementsViews discElementViews = new ElementsViews(discElement);

                listOfUserControls2.Children.Add(discElementViews);

                discElementViews.checkedChangeEv += CheckedChangeEv;
            }
        }
        /// <summary>
        /// Sortujemy liste po nazwie (A-Z)
        /// </summary>

        private void button8_Click(object sender, RoutedEventArgs e)
        {
            MyDirectory myDirectory1 = new MyDirectory(directoryPath.Text);

            listOfUserControls1.Children.Clear();

            List <DiscElement> subElements1 = myDirectory1.GetDiscElements();

            subElements1.Sort((x, y) => string.Compare(x.Name, y.Name));

            foreach (DiscElement discElement in subElements1)
            {
                ElementsViews discElementViews = new ElementsViews(discElement);

                listOfUserControls1.Children.Add(discElementViews);

                discElementViews.checkedChangeEv += CheckedChangeEv;
            }
        }
        /// <summary>
        /// otwieramy zaznaczony folder i wyświetlamy jego podfoldery i i pliki
        /// jeżeli zaznaczony element jest plikiem otwieramy go za pomocą domyślnego edytora
        /// </summary>

        private void button_Click(object sender, RoutedEventArgs e)
        {
            foreach (object ob in listOfUserControls1.Children)
            {
                ElementsViews elementViews = (ElementsViews)ob;
                if (elementViews.checkBox.IsChecked.Value)

                {
                    if (elementViews.discElement is MyDirectory)
                    {
                        string path = elementViews.discElement.Path;
                        try
                        {
                            Open_new_folder1(path);
                        }
                        catch (Exception)
                        {
                            MessageBox.Show("Brak dostępu do folderu");
                        }
                    }

                    else
                    {
                        string path = elementViews.discElement.Path;
                        try
                        {
                            System.Diagnostics.Process.Start(path);
                        }
                        catch (Exception)
                        {
                            MessageBox.Show("wystąpił błąd w trakcie otwierania pliku");
                        }
                    }
                    break;
                }
            }
            //---------------------------------------------
            foreach (object ok in listOfUserControls2.Children)
            {
                ElementsViews elementViews = (ElementsViews)ok;
                if (elementViews.checkBox.IsChecked.Value)

                {
                    if (elementViews.discElement is MyDirectory)
                    {
                        string path = elementViews.discElement.Path;
                        try
                        {
                            Open_new_folder2(path);
                        }
                        catch (Exception)
                        {
                            MessageBox.Show("brak dostępu do folderu");
                        }
                    }
                    else
                    {
                        string path = elementViews.discElement.Path;
                        try
                        {
                            System.Diagnostics.Process.Start(path);
                        }
                        catch (Exception)
                        {
                            MessageBox.Show("wystąpił błąd w trakcie otwierania pliku");
                        }
                    }
                    break;
                }
            }
        }