Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit 048134b

Browse files
fix #538: WPF Commands do not work properly in Windows Forms ContextMenus
1 parent bec0f8f commit 048134b

3 files changed

Lines changed: 17 additions & 3 deletions

File tree

src/Main/ICSharpCode.Core.WinForms/Menu/MenuCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ public virtual void UpdateStatus()
100100
} catch (ResourceNotFoundException) {}
101101
}
102102
Visible = GetVisible();
103-
Enabled = command != null && command.CanExecute(caller);
103+
command = CommandWrapper.Unwrap(command);
104+
Enabled = command != null && MenuService.CanExecuteCommand(command, caller);
104105
}
105106
}
106107

src/Main/ICSharpCode.Core.WinForms/Menu/MenuService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ namespace ICSharpCode.Core.WinForms
2828
public static class MenuService
2929
{
3030
public static Action<System.Windows.Input.ICommand, object> ExecuteCommand;
31+
public static Func<System.Windows.Input.ICommand, object, bool> CanExecuteCommand;
3132

3233
public static void AddItemsToMenu(ToolStripItemCollection collection, object owner, string addInTreePath)
3334
{

src/Main/SharpDevelop/Workbench/WpfWorkbench.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ public void Initialize()
137137
dockPanel.Children.Insert(dockPanel.Children.Count - 2, statusBar);
138138

139139
Core.WinForms.MenuService.ExecuteCommand = ExecuteCommand;
140+
Core.WinForms.MenuService.CanExecuteCommand = CanExecuteCommand;
140141
UpdateMenu();
141142

142143
AddHandler(Hyperlink.RequestNavigateEvent, new RequestNavigateEventHandler(OnRequestNavigate));
@@ -160,9 +161,9 @@ void ExecuteCommand(ICommand command, object caller)
160161
{
161162
ServiceSingleton.GetRequiredService<IAnalyticsMonitor>()
162163
.TrackFeature(command.GetType().FullName, "Menu");
163-
var routedCommand = command as System.Windows.Input.RoutedCommand;
164+
var routedCommand = command as RoutedCommand;
164165
if (routedCommand != null) {
165-
var target = System.Windows.Input.FocusManager.GetFocusedElement(this);
166+
var target = FocusManager.GetFocusedElement(this);
166167
if (routedCommand.CanExecute(caller, target))
167168
routedCommand.Execute(caller, target);
168169
} else {
@@ -171,6 +172,17 @@ void ExecuteCommand(ICommand command, object caller)
171172
}
172173
}
173174

175+
bool CanExecuteCommand(ICommand command, object caller)
176+
{
177+
var routedCommand = command as RoutedCommand;
178+
if (routedCommand != null) {
179+
var target = FocusManager.GetFocusedElement(this);
180+
return routedCommand.CanExecute(caller, target);
181+
} else {
182+
return command.CanExecute(caller);
183+
}
184+
}
185+
174186
// keep a reference to the event handler to prevent it from being garbage collected
175187
// (CommandManager.RequerySuggested only keeps weak references to the event handlers)
176188
EventHandler requerySuggestedEventHandler;

0 commit comments

Comments
 (0)