How to create a dropdown menu in ShowUI

Jul 31, 2011 at 4:40 PM

any help or links/resources?

Developer
Jul 31, 2011 at 5:35 PM

Check out the SimpleEditor.ps1 under ShowUI\Examples. 

Let me know if that is enough or not.

Doug

Developer
Jul 31, 2011 at 5:39 PM

Sorry about that, I was looking at different repository. Try this:

 

DockPanel -ControlName Editor {
  Menu -Dock Top -Height 20 {
     MenuItem -Header "_File" {
        ## Hook up the "New" menuitem to the New command ...
        MenuItem -Command 'New'
        MenuItem -Command 'Save'
        Separator
        ## There's still no "Exit" command (since Alt+F4 is handled by the OS)
        MenuItem -Header "E_xit" -On_Click { Close-Control $Editor }
     }
  }

  TextBox -Name "Content" -FontFamily "Consolas, Global Monospace" `
          -MinLines 10 -MinWidth 250 -AcceptsReturn -AcceptsTab
} -CommandBindings {
  # The "New" command (using this binds CTRL+N)
  CommandBinding -Command New -On_Executed   { $Content.Text = "" }
  # The "Save" command (using this binds CTRL+S)
  CommandBinding -Command Save `
                 -On_CanExecute { $_.CanExecute = $Content.Text.Length } `
                 -On_Executed   { Set-UIValue $Editor $Content.Text }
} -Show

Jul 31, 2011 at 5:44 PM
Edited Jul 31, 2011 at 6:10 PM

i do now see the new and save items. im trying to convert these to something more generic like applying to a variable or something along those lines.

The examples folder is very helpful for other features. thank you for that....

Aug 1, 2011 at 12:32 PM

Hi,

the key shorcuts (Ctrl-N, Ctrl-S) don't seem to work in the previous example. How are they registred anyway? I've tryed with -InputBindings {KeyBing -command New -Key "O" -modifieres Control } with same results.
I've found that this example gets very easily broken. For example:

Import-Module showUI

DockPanel -ControlName Editor {
  Menu -Dock Top -Height 20 {
     MenuItem -Header "_File" {
        ## Hook up the "New" menuitem to the New command ...
        MenuItem -Command 'New'
        MenuItem -Command 'Save'
        MenuItem  -Command 'Doit'
        Separator
        ## There's still no "Exit" command (since Alt+F4 is handled by the OS)
        MenuItem -Header "E_xit" -On_Click { Close-Control $Editor }
     }
  }

  TextBox -Name "Content" -FontFamily "Consolas, Global Monospace" `
          -MinLines 10 -MinWidth 250 -AcceptsReturn -AcceptsTab
} -CommandBindings {
  # The "New" command (using this binds CTRL+N)
  CommandBinding -Command New -On_Executed   { $Content.Text = "" }
  # The "Save" command (using this binds CTRL+S)
  CommandBinding -Command Save `
                 -On_CanExecute { $_.CanExecute = $Content.Text.Length } `
                 -On_Executed   { Set-UIValue $Editor $Content.Text }

   #same command binding as previous one (why not?)
  CommandBinding -Command Doit `
                 -On_CanExecute { $_.CanExecute = $Content.Text.Length } `
                 -On_Executed   { Set-UIValue $Editor $Content.Text }                 
} -Show

doesn't run, why? Here's the trace (sorry in French, but they might be easily translated)

New-DockPanel : Les méthodes WriteObject et WriteError ne peuvent pas être appelées après la fermeture du pipeline. Contactez les services de support technique Microsoft.
Au niveau de menuTest.ps1 : 3 Caractère : 10
+ DockPanel <<<<  -ControlName Editor {
    + CategoryInfo          : NotSpecified: (:) [New-DockPanel], CmdletInvocationException
    + FullyQualifiedErrorId : EmbeddedProcessRecordError,AutoGenerateCmdlets1825003516.NewDockPanelCommand

Thanks in advance,

Best regards,

jucg

Coordinator
Aug 1, 2011 at 1:58 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Coordinator
Aug 1, 2011 at 2:09 PM

OK, so, if you read the issue I just created, you'll see that the problem here is our error handling is obscuring the real error.  Look at $Error  (in this case, $Error[3] ... but the simplest thing to do is run $Error.Clear() before you run your command, and then look at $Error afterwards). 

The problem is that you just made up a command, and you can't do that. Commands are actually objects that implement "System.Windows.Input.ICommand" ... since it's very hard to write new classes in PowerShell, we assume that you're just going to use the Built-in commands, so we added support for automatically looking up by name the ones that are built into WPF, but you still can't just make up names ;-)

WPF Commanding Overview

 

Aug 3, 2011 at 7:02 AM

Thank you, 

you've made everything clear -even for a dummy like me!

I didn't know about built in commands and it's much clearer. If I understand well, one cannot make any new command directly in PowerShell. You would recommand using functions ( basically: MenuItem "hi" -Name "mi_Hi" -On_Click { myFunction } ).

Thanks also for the light on $Error. I think $Error is not iterable. I've tried using -ErrorVariable which is an ArrayList, but only one exception is captured.

Regards,

jucg

Coordinator
Aug 3, 2011 at 1:29 PM
Edited Aug 3, 2011 at 1:41 PM

For what it's worth ... I would recommend using the built in commands, if you can find one that matches what you want to do. Obviously if you can't, then you'll have to make up your own stuff.  The command library consists of all the commands in the following classes: ApplicationCommands, NavigationCommands, MediaCommands, EditingCommands, and the ComponentCommands.

The reason is that many of these commands include a set of default input bindings. For example, if you specify that your application handles the copy command, you automatically get the text ("Copy" in English), and the keyboard binding "CTRL+C", and the menu item has the "C" as the activation key ... but you also get bindings for other input devices, such as Tablet PC pen gestures and speech information.

Most of the built-ins are like that (providing hotkey, and LOCALIZED text), and there is a lot of logic and code built into them: a lot of them even have functionality that's implemented in other controls.  Check this out:

New-DockPanel { 
New-TextBox -name content -height 200 -width 300 -Dock Bottom -OutVariabe tb
New-MenuItem -Command Copy -CommandTarget $tb[0] -Dock Top
} -Show

Even without writing a CommandBinding, if you run that ... you'll see the menu item is disabled --and when you type some text in the textbox, and then SELECT the text (with the keyboard OR mouse)-- you'll see the menu item become enabled! If you click it, you'll notice it actually copies!  Writing all that enable/disable logic plus the actual copy code into a menu item would be a lot of work ;-)

As a postscript: if you're using third-party controls (via our Add-UIModule command) they typically implement the common commands as well, and may sometimes implement their own commands -- which might be picked up by our code-generation automatically, but may need some help.  In any case, you can use custom third party commands by sticking them in the parameter. It would look something like this: MenuItem -Command ([System.Windows.Input.ApplicationCommands]::Copy) ...

 

By the way, $Error is an automatic variable that's always an ArrayList, so it *is* iterable and indexable (is that the right word for a collection that can be accessed by index?)