private void SettingsWindow_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) { if (IsVisible) { if (FirstLoad) { this.ApplyTemplateRecursively(); MyControl.ApplyTemplateRecursively(); MainWindow = Owner as MainWindow; Dispatcher.Invoke(new Action(() => { if (FirstLoad) { //MyImageFileSelector.MainWindow = MainWindow; //MyAudioFileSelector.MainWindow = MainWindow; //MyAudioFileSelector.FilePathChanged += AudioFileChooser1_FilePathChanged; if (MyControl.MyItemsControl.ItemsSource == null) { // ? } else { var v = (CollectionView)CollectionViewSource.GetDefaultView(MyControl.MyItemsControl.ItemsSource); v.SortDescriptions.Add(new System.ComponentModel.SortDescription("Category", System.ComponentModel.ListSortDirection.Ascending)); v.GroupDescriptions.Add(new PropertyGroupDescription("Category") { }); } FirstLoad = false; } }), System.Windows.Threading.DispatcherPriority.Loaded); } if (MyControl.MyItemsControl.ItemsSource == null) { // ? } else { Dispatcher.Invoke(new Action(() => { MyControl.LoadValuesToBeEditedFromDb(); MyControl.CbAutoOpenLastFile.Text = Properties.Settings.Default.AutoOpenLastFile; MyControl.CbStartProgramWithWindows.IsChecked = Utils.ProgramStartsWithWindows(); MyControl.DgBeeps.Beeps.Clear(); foreach (Beep b in MainWindow.VM.Beeps) { MyControl.DgBeeps.Beeps.Add(b); } }), System.Windows.Threading.DispatcherPriority.Loaded); } } else { MyControl.ApplyTemplateRecursively(); MyControl.MyAudioFileSelector.AudioPlayer.StopSound(); } }