3

I am working on implementing global shortcut keys (i.e. application wide shortcut keys) for my WPF application, which has multiple windows. To achieve that, I am doing:

CommandManager.RegisterClassInputBinding(typeof(Window), o); // o is just a keybinding

That is, I am trying to register a keybinding with the Window class so that my shortcut key works, no matter what window is active. But my code throws the following exception on reaching this line:

System.InvalidOperationException was unhandled by user code

Message=This Freezable cannot be frozen.

Source=WindowsBase

StackTrace:

at System.Windows.Freezable.Freeze()
at System.Windows.Input.CommandManager.RegisterClassInputBinding(Type type, InputBinding inputBinding)

This is how the keybinding o is created:

KeyBinding o = new KeyBinding() 
{ 
  Command = f, 
  CommandParameter = popup, 
  Key = Key.Q, 
  Modifiers = ModifierKeys.Control 
}; 

popup is just a wpf popup. f is an object of a class that implements ICommand interface.

I have looked up similar questions on StackOverflow and they seem to be caused by the freezable object SolidColorBursh. I don't think that that applies to my case. Does anyone know what is going on?

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
jay electricity
  • 299
  • 5
  • 15
  • What is the stack trace of that exception, does that give you any clues as to what was attempted frozen? – Lasse V. Karlsen Oct 09 '13 at 16:17
  • Can you post the code of how `o` is created? `InputBinding` inherits from `Freezeable` ([MSDN link](http://msdn.microsoft.com/en-us/library/system.windows.input.inputbinding.aspx)), and since your exception is cluing us in there, I would suspect that is where the problem may be. – pineconesundae Oct 09 '13 at 16:49
  • Without an [mcve], there's no way to say exactly what the problem is here. No relevant or useful answer can be provided to the question. See duplicate for an answer that describes how _generally_ one can debug this exception when the obvious restrictions don't seem to apply. The trace output will clarify what restriction was in fact violated. – Peter Duniho Apr 03 '21 at 22:26

1 Answers1

0

I ran into this problem today and found the issue. A Freezable object cannot be frozen if any of the following are true (from the documentation linked below):

  • It has animated or data bound properties.
  • It has properties set by a dynamic resource.
  • It contains Freezable sub-objects that can't be frozen.

The way I read that is basically that if you're binding to any of the object's properties (the Window in your case) or if any of its children's properties (or children's children's properties, etc.) have any data binding, it can't be frozen which makes a number of operations impossible.

In my case I was trying to use an EventTrigger to set a value on a property of another object. I had that property bound to my view model and so it failed for the reasons above. (I was trying to sneak some data out of the view without doing things more appropriately by creating an attached DependencyProperty, which is what I ultimately ended doing).

The Microsoft documentation about this is here: https://learn.microsoft.com/en-us/dotnet/desktop/wpf/advanced/freezable-objects-overview

MikeBMcL
  • 414
  • 3
  • 6