Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > Avalon

Vista - RoutedCommand doesn't route in UI tree

 
 
Old 05-01-2006   #1 (permalink)
Daniel


 
 

RoutedCommand doesn't route in UI tree

Hi,

I am learning RoutedCommand and CommandBinding in WPF.
I wrote a small test program as following, I hope when I put focus in the
textbox and
invoke my custom command with "ALT-C" the execute event will be routed from
the
textbox to the root window, however the execute event stops at the textbox
and does
not route to the window.

Can anyone tell me why?

<Window x:Class="WindowsApplication3.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WindowsApplication3" Height="300" Width="300"
Loaded="OnLoaded">
<StackPanel>
<TextBox x:Name="txt1">Foo</TextBox>
</StackPanel>
</Window>

namespace WindowsApplication3
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>

public partial class Window1 : Window
{
public static RoutedCommand myCommand;

public Window1()
{
InitializeComponent();
}

private void OnLoaded(Object sender, RoutedEventArgs e)
{

myCommand = new RoutedUICommand("myCommand", "myCommand",
typeof(Window1));
myCommand.InputGestures.Add(new KeyGesture(Key.C,
ModifierKeys.Alt));

CommandBinding cmdBindingWindow = new CommandBinding(myCommand);
cmdBindingWindow.Executed += NewCommandHandlerWindow;
this.CommandBindings.Add(cmdBindingWindow);

CommandBinding cmdBindingTxt = new CommandBinding(myCommand);
cmdBindingTxt.Executed += NewCommandHandlerTxt;
txt1.CommandBindings.Add(cmdBindingTxt);
}

private void NewCommandHandlerWindow(Object sender,
ExecutedRoutedEventArgs e)
{
Debug.WriteLine("NewCommandHandlerWindow");
}

private void NewCommandHandlerTxt(Object sender,
ExecutedRoutedEventArgs e)
{
Debug.WriteLine("NewCommandHandlerTxt");
}
}
}

My System SpecsSystem Spec
Old 05-01-2006   #2 (permalink)
Nick Kramer [MSFT]


 
 

Re: RoutedCommand doesn't route in UI tree

Because you have a command handler attached to the text box, we assume the
command is handled, and don't route further.

-Nick Kramer [MSFT]
http://blogs.msdn.com/nickkramer

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

"Daniel" <Daniel@discussions.microsoft.com> wrote in message
news:92671C94-DD95-456F-8E6D-5C8794145340@microsoft.com...
> Hi,
>
> I am learning RoutedCommand and CommandBinding in WPF.
> I wrote a small test program as following, I hope when I put focus in the
> textbox and
> invoke my custom command with "ALT-C" the execute event will be routed
> from
> the
> textbox to the root window, however the execute event stops at the textbox
> and does
> not route to the window.
>
> Can anyone tell me why?
>
> <Window x:Class="WindowsApplication3.Window1"
> xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
> xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
> Title="WindowsApplication3" Height="300" Width="300"
> Loaded="OnLoaded">
> <StackPanel>
> <TextBox x:Name="txt1">Foo</TextBox>
> </StackPanel>
> </Window>
>
> namespace WindowsApplication3
> {
> /// <summary>
> /// Interaction logic for Window1.xaml
> /// </summary>
>
> public partial class Window1 : Window
> {
> public static RoutedCommand myCommand;
>
> public Window1()
> {
> InitializeComponent();
> }
>
> private void OnLoaded(Object sender, RoutedEventArgs e)
> {
>
> myCommand = new RoutedUICommand("myCommand", "myCommand",
> typeof(Window1));
> myCommand.InputGestures.Add(new KeyGesture(Key.C,
> ModifierKeys.Alt));
>
> CommandBinding cmdBindingWindow = new
> CommandBinding(myCommand);
> cmdBindingWindow.Executed += NewCommandHandlerWindow;
> this.CommandBindings.Add(cmdBindingWindow);
>
> CommandBinding cmdBindingTxt = new CommandBinding(myCommand);
> cmdBindingTxt.Executed += NewCommandHandlerTxt;
> txt1.CommandBindings.Add(cmdBindingTxt);
> }
>
> private void NewCommandHandlerWindow(Object sender,
> ExecutedRoutedEventArgs e)
> {
> Debug.WriteLine("NewCommandHandlerWindow");
> }
>
> private void NewCommandHandlerTxt(Object sender,
> ExecutedRoutedEventArgs e)
> {
> Debug.WriteLine("NewCommandHandlerTxt");
> }
> }
> }



My System SpecsSystem Spec
Old 05-01-2006   #3 (permalink)
Drew Marsh


 
 

Re: RoutedCommand doesn't route in UI tree

Nick Kramer wrote:

> Because you have a command handler attached to the text box, we assume
> the command is handled, and don't route further.


Shouldn't there be a feature parallel to AddHandler's handledEventsToo for
commands? Maybe handledCommandsToo?

-Drew


My System SpecsSystem Spec
Old 05-01-2006   #4 (permalink)
Daniel


 
 

Re: RoutedCommand doesn't route in UI tree

Thanks for the help.

But still the problem is that :
> Because you have a command handler attached to the text box, we assume the
> command is handled, and don't route further.


Even if I mark the execute event as NOT handled in the that handler,
the event still not get routed, for example like following:

private void NewCommandHandlerTxt(Object sender,
ExecutedRoutedEventArgs e)
{
Debug.WriteLine("NewCommandHandlerTxt");
e.Handled = false;
}
My System SpecsSystem Spec
Old 05-02-2006   #5 (permalink)
Marcus


 
 

Re: RoutedCommand doesn't route in UI tree

What Nick meant is that as soon as a handler is added for a command,
the framework will mark the orignal routed event as handled as raise
another event, directly aimed at calling your handler...

In fact, if you add a handler for a command, it is to execute the
command... therefore, command has reached it`s target (no more routing,
we don`t want more than one people to execute "copy" [example] at the
same time)...

My System SpecsSystem Spec
Old 05-03-2006   #6 (permalink)
Nick Kramer [MSFT]


 
 

Re: RoutedCommand doesn't route in UI tree

Could be. I've been a little reluctant to go there -- there's no xaml
version of handledEventsToo so it felt a little weird having a xaml
handledCommandsToo. And I don't want to bloat commanding too much, it's
meant to be a convenience wrapper around input rather than the end all be
all solution. But, I wouldn't be surprised if we do it at some point.

-Nick Kramer [MSFT]
http://blogs.msdn.com/nickkramer

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

"Drew Marsh" <drub0y@hotmail.no.spamming.com> wrote in message
news:f01844f1f2c008c83b847f474efa@msnews.microsoft.com...
> Nick Kramer wrote:
>
>> Because you have a command handler attached to the text box, we assume
>> the command is handled, and don't route further.

>
> Shouldn't there be a feature parallel to AddHandler's handledEventsToo for
> commands? Maybe handledCommandsToo?
>
> -Drew
>
>



My System SpecsSystem Spec
Old 05-16-2006   #7 (permalink)
Daniel


 
 

Re: RoutedCommand doesn't route in UI tree

Ok, I've got it. Thanks a lot.

"Marcus" wrote:

> What Nick meant is that as soon as a handler is added for a command,
> the framework will mark the orignal routed event as handled as raise
> another event, directly aimed at calling your handler...
>
> In fact, if you add a handler for a command, it is to execute the
> command... therefore, command has reached it`s target (no more routing,
> we don`t want more than one people to execute "copy" [example] at the
> same time)...
>
>

My System SpecsSystem Spec
 

Thread Tools


Similar Threads
Thread Forum
Add ip route to a computer. VB Script
Blocking IPs with route add Vista General
Add Route Vista General
route add vista Vista networking & sharing
Help: Route Add Failed? Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46