1

In WPF, the RoutedUICommand is not a dependency object, nor does it implement INotifyPropertyChanged meaning if you were to bind its Text property to something in the UI such as a MenuItem's header, and Mode wasn't OneTime, you'd potentially get a memory leak with dynamically loaded context menus or UIs where items get created and destroyed very frequently as those UIs are loaded and unloaded.

Conversely, if you do use OneTime, you'll avoid the leak, but you'll lose change notification which we need as our menu's text needs to dynamically update as per status in the application. (Think how a typical "Save 'xxx' As..." command changes to reflect the current item's name.)

That said, how do we properly bind to text and get the changes without causing memory leaks?

Our proposed solution is to create a subclass of RoutedCommand (note: no 'UI') where we create our own Text property as well as implement INotifyPropertyChanged to support it. However we're not sure if there will be issues with WPF's built-in support for things like automatically binding a MenuItem's header to the Text property since it isn't a RoutedUICommand. (Note, we could just use a real RoutedUICommand object, then 'new over' the Text property but there you can run into issues with how you access the property so we'd rather avoid that if possible.)

Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
  • Why not explicitly clear the binding? BindingOperations.ClearBinding(MyTextBox, TextBlock.TextProperty); – Aaron McIver Oct 25 '11 at 16:26
  • Um... ya lost me there. Not sure I was clear enough. This is for a RoutedUICommand that appears in a menu or a context menu where we want the text in the menu to change when the text in the RoutedUICommand changes. But as I said, RoutedUICommand doesn't support any change notifications, nor is it a dependency object so we're not sure how to avoid the leak. This doesn't have anything to do with clearing a binding. Actually, that's the exact opposite of what we want. We *want* the binding... just without the leak. – Mark A. Donohoe Oct 25 '11 at 16:35
  • It's unclear what you're trying to achieve, can you post some code? – Louis Kottmann Oct 25 '11 at 16:38
  • @MarqueIV The binding is only an issue when you no longer need it. What constitutes _no longer needing it_ will vary. Moving out of scope is a typical scenario and in this case you would simply call ClearBinding to alleviate the potential leak. It's not a leak if the binding is in use, it's a leak when the binding is no longer needed. – Aaron McIver Oct 25 '11 at 16:43
  • @Aaron, as I stated, these are dymnamic UIs, and trying to implement a disposing pattern where we manually clear the bindings on every single menu item (or anything that binds to a RoutedUICommand's text for that matter) isn't really feasible because there are over a hundred menu items and commands in the app (again, the UI is dynamic.) Plus, MenuItem does some auto-binding when its given a RoutedUICommand so I'm not sure if we can just clear that binding as I don't know how the framework implements it behind the scenes. – Mark A. Donohoe Oct 25 '11 at 16:52
  • @Baboon, not sure how to be more clear, but I did make some edits. There's no code to show as it would be a single binding call and that's not the issue. The leak is when you bind to an object that doesn't support change notification, nor is a dependency object. – Mark A. Donohoe Oct 25 '11 at 16:53

0 Answers0