![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Keystroke commands | Vista General | |||
| Commands | Vista General | |||
| DOS commands | Vista General | |||
| BCR using psh like commands | PowerShell | |||