Skip to content

Commit

Permalink
Fix crash on opening collection context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
DSPaul committed Apr 20, 2024
1 parent b1d0fcb commit ed55855
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
12 changes: 12 additions & 0 deletions src/Converters/ToStringConverter.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
4 changes: 3 additions & 1 deletion src/Windows/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
PreviewKeyDown="Window_PreviewKeyDown">
<Window.Resources>
<converters:WindowStateToBoolConverter x:Key="IsMaximizedConverter"/>
<converters:ToStringConverter x:Key="ToString"/>
<DataTemplate DataType="{x:Type layouts:ListLayoutViewModel}">
<views:ListLayout DataContext="{Binding}"/>
</DataTemplate>
Expand All @@ -39,6 +40,7 @@
<DataTemplate DataType="{x:Type viewmodels:CodexInfoViewModel}">
<views:CodexInfoView DataContext="{Binding}"/>
</DataTemplate>

</Window.Resources>
<Window.Style>
<Style>
Expand Down Expand Up @@ -235,7 +237,7 @@
<Style TargetType="MenuItem" BasedOn="{StaticResource {x:Type MenuItem}}">
<Setter Property="MenuItem.Command" Value="{Binding Path=DataContext.MergeCollectionIntoCommand,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MenuItem}}}"/>
<Setter Property="MenuItem.CommandParameter" Value="{Binding Header, RelativeSource={RelativeSource self}}"/>
<Setter Property="MenuItem.CommandParameter" Value="{Binding Header, RelativeSource={RelativeSource self}, Converter={StaticResource ToString}}"/>
</Style>
</MenuItem.ItemContainerStyle>
</MenuItem>
Expand Down
16 changes: 13 additions & 3 deletions src/Windows/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using COMPASS.ViewModels;
using COMPASS.Tools;
using COMPASS.ViewModels;
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ed55855

Please sign in to comment.