From ed55855eac7f593ee21ba8b567a09e2388b87500 Mon Sep 17 00:00:00 2001 From: DSPAUL Date: Sat, 20 Apr 2024 16:47:19 +0200 Subject: [PATCH] Fix crash on opening collection context menu --- src/Converters/ToStringConverter.cs | 12 ++++++++++++ src/Windows/MainWindow.xaml | 4 +++- src/Windows/MainWindow.xaml.cs | 16 +++++++++++++--- 3 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 src/Converters/ToStringConverter.cs diff --git a/src/Converters/ToStringConverter.cs b/src/Converters/ToStringConverter.cs new file mode 100644 index 00000000..0af60461 --- /dev/null +++ b/src/Converters/ToStringConverter.cs @@ -0,0 +1,12 @@ +using System; +using System.Globalization; +using System.Windows.Data; + +namespace COMPASS.Converters +{ + internal class ToStringConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) => value as string ?? ""; + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotImplementedException(); + } +} diff --git a/src/Windows/MainWindow.xaml b/src/Windows/MainWindow.xaml index b5f3b185..726668a5 100644 --- a/src/Windows/MainWindow.xaml +++ b/src/Windows/MainWindow.xaml @@ -21,6 +21,7 @@ PreviewKeyDown="Window_PreviewKeyDown"> + @@ -39,6 +40,7 @@ + diff --git a/src/Windows/MainWindow.xaml.cs b/src/Windows/MainWindow.xaml.cs index d8302d2c..438c79d0 100644 --- a/src/Windows/MainWindow.xaml.cs +++ b/src/Windows/MainWindow.xaml.cs @@ -1,4 +1,5 @@ -using COMPASS.ViewModels; +using COMPASS.Tools; +using COMPASS.ViewModels; using System; using System.ComponentModel; using System.Runtime.InteropServices; @@ -171,8 +172,17 @@ private async void CloseButton_Click(object sender, RoutedEventArgs e) private void Toggle_ContextMenu(object sender, RoutedEventArgs e) { - ((Button)sender).ContextMenu!.PlacementTarget = (Button)sender; - ((Button)sender).ContextMenu!.IsOpen = !((Button)sender).ContextMenu!.IsOpen; + try + { + Button btn = (Button)sender; + btn.ContextMenu!.PlacementTarget = btn; + btn.ContextMenu!.IsOpen = !btn.ContextMenu!.IsOpen; + } + catch (Exception ex) + { + Logger.Error("Could not open collection Context menu", ex); + return; + } } private async void Window_PreviewKeyDown(object sender, KeyEventArgs e)