1

I have several user controls which are instantiated in a ListBox. I have a button in my Window which raises NavigationCommands.Refresh, and a CanExecute handler to allow it to execute:

<Window.CommandBindings>
    <CommandBinding Command="NavigationCommands.Refresh" CanExecute="CanAlwaysExecute" />
</Window.CommandBindings>

In each of these dynamically created usercontrols (created inside the ListBox's ItemTemplate), I have a handler for NavigationCommands.Refresh. When I click the button in the window, should the handler I have set up in each of the usercontrols not fire? I need the command to tunnel down to each of the UCs but it's not.

<UserControl.CommandBindings>
    <CommandBinding Command="NavigationCommands.Refresh" Executed="UpdateStatus" CanExecute="CanAlwaysExecute" />
</UserControl.CommandBindings>
....
<Button Width="200" Text="Refresh All" Command="NavigationCommands.Refresh"/>
Echilon
  • 10,064
  • 33
  • 131
  • 217

1 Answers1

0

Tunneling commands do not work like that, they go from the root to the target site, they do not "split". So they start at the window and end at the button that was clicked.

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • @Echilon: Be creative. e.g. raise the command manually for all controls of interest if the main-button is clicked. – H.B. Jun 18 '12 at 11:16
  • That would require looping through all controls in the listbox, finding another ListBox which is nested inside that, then looping through all items in that and finding my usercontrol, then calling a method. Is there a more elegant way? – Echilon Jun 18 '12 at 11:19
  • @Echilon: Not that i know of. You should not need to traverse the UI though, in your model those associations should be more clear and any changes that the command makes should be reflected in the UI. Looping over data should not be much of a big deal. – H.B. Jun 18 '12 at 11:23