Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > Avalon

Help with commands in C++

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 07-07-2006   #1 (permalink)
=?Utf-8?B?Sm9obiBEdW5u?=
Guest


 

Help with commands in C++

I've been trying to understand how to implement commands in my C++/CLI
application. I've read the 'Commanding Overview' as well as all the How-to
Topics in the SDK and still have no idea how commands work.

It might be easest to explain what I'd like to do. I have an application
which instantiates a single window which contains a dock. The dock contains a
Menu, StatusBar and Grid. The Grid contains even more windows and somewhere
down the line is a Canvas which is my 'workspace'. I'd like to have menu
items which call commands on the Canvas - at some point there will be
multiple Canvas windows so it needs to get routed to the one with focus.

I've added MenuItems with the built in application commands like Open and
Copy. I then added a new CommandBinding with both an
ExecuteRouterEventHandler and CanExecuteRoutedEventHandler specified.
Unfortunately neither of the handlers ever get called and my MenuItem remains
grayed out.

I assume there's something I need to do to route the Menu commands to my
canvas but nowhere in the documentation do I see a hint about what to try.
There is a mention of adding CommandHandlers to the Application but the
Application doesn't have a CommandBindings to call Add() on.

This seems like a fairly basic and common task so I'm guessing that I'm
missing the obvious. If anyone can point me in the right direction I'd
appreciate it.

Thanks-

John

My System SpecsSystem Spec
Old 07-10-2006   #2 (permalink)
=?Utf-8?B?QnJpYW4gTG92ZSAtLSBNU0ZU?=
Guest


 

RE: Help with commands in C++

John,

Where do you have your CommandBinding defined? On the Menu? This may be
one of the problems. RoutedCommands bubble and tunnel through the element
tree like routed events. When a command is invoked, the routing starts at
either the element with Keyboard focus or the element that is set as the
CommandTarget.

So, two things that may be happening.

Since you are going to have multiple Canvas objects, setting the
CommandTarget to the Canvas isn't going to work. So, you'll want to have the
routing start with the element which has Keyboard focus. That means you need
to set IsFocusable on the Canvas to true. The default on Canvas is false.

The second thing is that if the CommandBinding is attached to the Menu, it
will never get routed from the Canvas. Because the Grid and the Menu or
siblings. To solve this, place the CommandBinding on the main Window.

Here's some code that works for me. Let me know if this doesn’t solve your
problem. I'll see that this scenario makes it into the SDK docs.

<Window x:Class="CommandFouemQuestion.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="CommandFouemQuestion" Height="300" Width="300"
>

<Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.Help"
CanExecute="CanExecuteHelp"
Executed="ExecutedHelp" />
</Window.CommandBindings>
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Command="ApplicationCommands.Help"/>
</Menu>
<Grid DockPanel.Dock="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Canvas Name="WorkSpaceCanvas"
Grid.Column="0"
Grid.Row="0"
Height="100"
Width="100"
Background="AliceBlue"
Focusable="True"/>
</Grid>
<StatusBar DockPanel.Dock="Bottom"></StatusBar>
</DockPanel>
</Window>

public void CanExecuteHelp(object sender, CanExecuteRoutedEventArgs e)
{
Canvas target = e.Source as Canvas;
if (target != null)
{
e.CanExecute = true;
}
}

public void ExecutedHelp(object sender, ExecutedRoutedEventArgs e)
{
Canvas target = e.Source as Canvas;
if (target != null)
{
target.Background = Brushes.Red;
}
}

"John Dunn" wrote:

> I've been trying to understand how to implement commands in my C++/CLI
> application. I've read the 'Commanding Overview' as well as all the How-to
> Topics in the SDK and still have no idea how commands work.
>
> It might be easest to explain what I'd like to do. I have an application
> which instantiates a single window which contains a dock. The dock contains a
> Menu, StatusBar and Grid. The Grid contains even more windows and somewhere
> down the line is a Canvas which is my 'workspace'. I'd like to have menu
> items which call commands on the Canvas - at some point there will be
> multiple Canvas windows so it needs to get routed to the one with focus.
>
> I've added MenuItems with the built in application commands like Open and
> Copy. I then added a new CommandBinding with both an
> ExecuteRouterEventHandler and CanExecuteRoutedEventHandler specified.
> Unfortunately neither of the handlers ever get called and my MenuItem remains
> grayed out.
>
> I assume there's something I need to do to route the Menu commands to my
> canvas but nowhere in the documentation do I see a hint about what to try.
> There is a mention of adding CommandHandlers to the Application but the
> Application doesn't have a CommandBindings to call Add() on.
>
> This seems like a fairly basic and common task so I'm guessing that I'm
> missing the obvious. If anyone can point me in the right direction I'd
> appreciate it.
>
> Thanks-
>
> John

My System SpecsSystem Spec
Old 07-20-2006   #3 (permalink)
=?Utf-8?B?Sm9obiBEdW5u?=
Guest


 

RE: Help with commands in C++

Thanks for the info. Unfortunately I wasn't able to get it to work. I set
Focusable = true in the constructor of my Canvas but I don't think it's
actually grabbing focus when the mouse is clicked inside of it. The treeview
on the left of my application is still showing that it has focus ( with a
dark gray hilight instead of the light gray 'non-focused' hilight ).

Also, when I handle my command the Source is the treeview and not the
Canvas. At first I had my Canvas inside of a ScrollViewer - when the command
was executed the ScrollViewer was the Source but never the Canvas.

Is there anymore that I need to do to make my Canvas grab focus? I was
handling OnMouse* but I've commented all of those out so they shouldn't be
causing any issues.

Thanks-

John

"Brian Love -- MSFT" wrote:

> John,
>
> Where do you have your CommandBinding defined? On the Menu? This may be
> one of the problems. RoutedCommands bubble and tunnel through the element
> tree like routed events. When a command is invoked, the routing starts at
> either the element with Keyboard focus or the element that is set as the
> CommandTarget.
>
> So, two things that may be happening.
>
> Since you are going to have multiple Canvas objects, setting the
> CommandTarget to the Canvas isn't going to work. So, you'll want to have the
> routing start with the element which has Keyboard focus. That means you need
> to set IsFocusable on the Canvas to true. The default on Canvas is false.
>
> The second thing is that if the CommandBinding is attached to the Menu, it
> will never get routed from the Canvas. Because the Grid and the Menu or
> siblings. To solve this, place the CommandBinding on the main Window.
>
> Here's some code that works for me. Let me know if this doesn’t solve your
> problem. I'll see that this scenario makes it into the SDK docs.
>
> <Window x:Class="CommandFouemQuestion.Window1"
> xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
> xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
> Title="CommandFouemQuestion" Height="300" Width="300"
> >

> <Window.CommandBindings>
> <CommandBinding Command="ApplicationCommands.Help"
> CanExecute="CanExecuteHelp"
> Executed="ExecutedHelp" />
> </Window.CommandBindings>
> <DockPanel>
> <Menu DockPanel.Dock="Top">
> <MenuItem Command="ApplicationCommands.Help"/>
> </Menu>
> <Grid DockPanel.Dock="Top">
> <Grid.ColumnDefinitions>
> <ColumnDefinition />
> </Grid.ColumnDefinitions>
> <Grid.RowDefinitions>
> <RowDefinition/>
> </Grid.RowDefinitions>
> <Canvas Name="WorkSpaceCanvas"
> Grid.Column="0"
> Grid.Row="0"
> Height="100"
> Width="100"
> Background="AliceBlue"
> Focusable="True"/>
> </Grid>
> <StatusBar DockPanel.Dock="Bottom"></StatusBar>
> </DockPanel>
> </Window>
>
> public void CanExecuteHelp(object sender, CanExecuteRoutedEventArgs e)
> {
> Canvas target = e.Source as Canvas;
> if (target != null)
> {
> e.CanExecute = true;
> }
> }
>
> public void ExecutedHelp(object sender, ExecutedRoutedEventArgs e)
> {
> Canvas target = e.Source as Canvas;
> if (target != null)
> {
> target.Background = Brushes.Red;
> }
> }
>
> "John Dunn" wrote:
>
> > I've been trying to understand how to implement commands in my C++/CLI
> > application. I've read the 'Commanding Overview' as well as all the How-to
> > Topics in the SDK and still have no idea how commands work.
> >
> > It might be easest to explain what I'd like to do. I have an application
> > which instantiates a single window which contains a dock. The dock contains a
> > Menu, StatusBar and Grid. The Grid contains even more windows and somewhere
> > down the line is a Canvas which is my 'workspace'. I'd like to have menu
> > items which call commands on the Canvas - at some point there will be
> > multiple Canvas windows so it needs to get routed to the one with focus.
> >
> > I've added MenuItems with the built in application commands like Open and
> > Copy. I then added a new CommandBinding with both an
> > ExecuteRouterEventHandler and CanExecuteRoutedEventHandler specified.
> > Unfortunately neither of the handlers ever get called and my MenuItem remains
> > grayed out.
> >
> > I assume there's something I need to do to route the Menu commands to my
> > canvas but nowhere in the documentation do I see a hint about what to try.
> > There is a mention of adding CommandHandlers to the Application but the
> > Application doesn't have a CommandBindings to call Add() on.
> >
> > This seems like a fairly basic and common task so I'm guessing that I'm
> > missing the obvious. If anyone can point me in the right direction I'd
> > appreciate it.
> >
> > Thanks-
> >
> > John

My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Keystroke commands SunnyAZ Vista General 1 03-25-2008 07:28 PM
Commands JMed Vista General 5 01-08-2008 07:50 AM
DOS commands PJ Perillo Vista General 5 12-18-2007 02:31 PM
BCR using psh like commands William Stacey [C# MVP] PowerShell 2 01-21-2007 06:49 PM
Bug in WPF Commands ? jods Avalon 3 05-08-2006 04:00 PM


Vistax64.com 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 2005-2008

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 47 48 49 50 51