PowerShell Pro Tools
Ironman SoftwareGitHub
  • About
  • System Requirements
  • PowerShell Tools Documentation
    • Visual Studio
      • Analysis
      • Debugging
        • Local Debugging
        • Remote Debugging
      • Format Document
      • Go to Definition
      • Packaging in Visual Studio
      • PowerShell 7 Support
      • PowerShell Interactive Window
      • Project System
        • Advanced
        • Debug
        • Build Events
      • Settings
        • General
        • .editorconfig
        • Analysis
        • Diagnostics
      • Tool Windows
      • Refactoring
      • Unit Test Adapter
      • User Interface Design
        • Windows Forms
  • PowerShell Pro Tools Documentation
    • Visual Studio Code
      • Automating Visual Studio Code
      • Code Conversion
      • Debugging
        • Run in New Terminal
        • One-Click Attach
      • Decompiler
      • Diagnostics
      • Enhanced Hover
      • Generating a UI from a function
      • Generate a Tool from a Function
      • Packaging in Visual Studio Code
      • Pin Session
      • PowerShell Explorer
      • Profiler
      • Sign On Save
      • RapidSense
      • Refactoring
      • Rename Symbols
      • Quick Scripts
      • Windows Forms Designer
    • Installers
    • Packaging
      • Package.psd1
      • PowerShell Packager
      • Package Hosts
      • Package as Service
      • Packaging on Linux
      • Packaging on Mac OS X
      • Continuous Integration
      • Anti-Virus
    • PowerShell Module
      • Global Hotkeys
      • Show-PSEditor
      • Show-PSScriptPad
      • Show-TUIDesigner
      • ConvertTo-CSharp
      • ConvertTo-PowerShell
      • Merge-Script
      • Install-PoshProToolsLicense
      • about_MergeScriptConfig
      • Show-WinFormDesigner
    • PowerShell Protect
      • Getting Started
      • Installation
      • Actions
      • Rules
      • Configuration
    • PSCommander
    • PSScriptPad
    • Installation and Licensing
      • Visual Studio Offline Installation
  • Changelog
    • PowerShell Tools for Visual Studio
    • PowerShell Pro Tools for Visual Studio Code
    • PowerShell Packager
    • PSScriptPad
    • PowerShell Pro Tools Module
Powered by GitBook
On this page
  • Building a GUI with WPF in Visual Studio
  • Applies to:
  • Creating a Project
  • Wiring up Event Handlers
  • Packaging as an executable
  1. PowerShell Tools Documentation
  2. Visual Studio

User Interface Design

PreviousUnit Test AdapterNextWindows Forms

Last updated 10 months ago

Requires a

Building a GUI with WPF in Visual Studio

Applies to:

  • Visual Studio 2017, 2019, 2022

  • PowerShell Pro Tools license

Creating a Project

You can create a module or script project.

Click File->New->Project

Select the Module or Script project type, name it and then click Ok.

Right click on your project to create a new file and select the PowerShell WPF Window item template.

After creating your WPF window, you will see the same designer you would for a C# or VB.NET project.

A code behind file is automatically created that contains a PowerShell script capable of loading the XAML and running the WPF window.

Pressing F5 or clicking the Start button on the menu will launch your WPF window.

‘

Wiring up Event Handlers

In order to enable execution of actions based on different events, you’ll want to hook up some event handlers. To do so, the first thing you’ll need to do is name your control. Named controls are currently a requirement for event handlers.

Next, select the event you would like wired up, enter a name for the event handler function and press enter.

The code behind generator will create a bunch of code to wire your event handler function to your control.

The first section removes the actual event handler XML attribute from the XAML. This is because PowerShell doesn’t actually support this type of event binding. I’ll look into supporting it somehow in the future but for now the event handlers in the XAML are placeholders for what is created in the code behind. They are removed at runtime.

The next piece of generated code is for variables that define the controls within your XAML. Currently, only the control you are adding an event handler for is generated in this section. In the future, all named controls will show up here. This allows PowerShell scripts to interact with the live controls.

Finally, we create an event handler function and wire it to the event on the control.

From here, we can define logic in the onClick function to take actions like opening a message box.

Packaging as an executable

XAML and the code behind script can be. Simply right click on the WPF XAML window in the Solution Explorer window and select “Package as executable”.

packaged as an executable
PowerShell Pro Tools license.