Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
CommandRouting using a button.
Message
From
12/04/2008 01:24:07
 
 
General information
Forum:
ASP.NET
Category:
Windows Presentation Foundation (WPF)
Miscellaneous
Thread ID:
01309791
Message ID:
01310149
Views:
16
>When I first tried your code my question was not why the windows level button didn't work - but why the ToolBar/MenuItem buttons *did*
>Turns out the Toolbar and Menu have special functionality that automatically sets the CommandTarget to the control with focus. To get the windows button to fire the event in this scenario you need to set its CommandTarget property explicitly:
CommandTarget="{Binding ElementName=userControl}"
I was aware of that but didn't use it because moving the bindings worked fine for my needs. It also only travels the tree from the point you bind it to up to the root and back so it won't change with the focus. But the way you worded that got me thinking and...

Woot we have a solution:
this.PreviewGotKeyboardFocus += new KeyboardFocusChangedEventHandler(Window1_PreviewGotKeyboardFocus);

void Window1_PreviewGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
  {
  Button2.CommandTarget = e.NewFocus;
  }
Now it works just like the toolbar or the menu. Thanks!

>BTW this info found in the Apress 'WPF in C#2008' which turned up today - looks good; certainly better coverage on Commands than from WPF Unleashed.
>HTH,
>Viv

Cool once you get a better feel for them let me know which you like best. I have Applications = Code + Markup coming but the XAML purist crowd don't seem to like that one.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform