I have done so much automation using winforms powershell, now I am slowly moving my scripts to WPF, unlike my earlier article Powershell WPF GUI: ToolBox control Textbox watermark placeholder demo here in this article I will show how to create LinkLabel in WPF. When I was going through Microsoft official documentation Windows Forms Controls and Equivalent WPF Controls, after comparing winforms, I found there isn't equivalent linklabel control in WPF but instead they have given Hyperlink class to host hyperlinks with flow content.
I am using here two approach to get my hyperlink working, first is using Hyperlink and another is using mouse event triggers.
Here in first demo I have directly modified my XAML content by adding Hyperlink class, (Note: Label is closed after Hyperlink). Next in I used event PreviewMouseDown, this determines what happens when Link url is clicked. Preview events, also known as tunneling events, are routed events where the direction of the route travels from the application root towards the element that raised the event and is reported as the source in event data.
Additionally I have added Tooltip to show useful popup info, which looks nice. Bold highlighted contents are the modified and newly added by me.
<Label x:Name="Link1" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="45" FontSize='14' ToolTip='vmware'>
<Hyperlink NavigateUri="http://www.vmware.com">Link1</Hyperlink>
</Label>
#Link1 - Mouse button event
$Link1.Add_PreviewMouseDown({[system.Diagnostics.Process]::start('http://www.vmware.com')})
In my second demo, I am using complete event based hyperlink label, Bold highlighted the items added extra by me, When WPF form is loaded it shows the font color (foreground) DarkBlue, Cursor is Hand.
Below are the simple label events used in the scripts.
1) Add_MouseLeftButtonUp - This method defines what will happened when you press left mouse click on link.
2) Add_MouseEnter - In this first part method I created mouse hover effect, when mouse enters on label, color is changed to purple
3) Add_MouseLeave - this is second part of mouse hover effect, and when mouse is leaved or moved from label, color is changed back to DarkBlue.
https://msdn.microsoft.com/en-us/library/system.windows.uielement.mouseleftbuttonup%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
https://msdn.microsoft.com/en-us/library/system.windows.uielement.mouseenter(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.windows.uielement.mouseleave(v=vs.110).aspx
<Label x:Name="Link2" Content="Link2" HorizontalAlignment="Left" Margin="10,41,0,0" VerticalAlignment="Top" Width="45" FontSize='14' Foreground='DarkBlue' Cursor="Hand" ToolTip='Google'/>
#Link2 - My custom mouse button howver and click event
$Link2.Add_MouseLeftButtonUp({[system.Diagnostics.Process]::start('http://www.google.com')})
$Link2.Add_MouseEnter({$Link2.Foreground = 'Purple'})
$Link2.Add_MouseLeave({$Link2.Foreground = 'DarkBlue'})
Download this demo script here, It is also available on GitHub.com.
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 |
#Load required libraries Add-Type -AssemblyName PresentationFramework, PresentationCore, WindowsBase, System.Windows.Forms, System.Drawing [xml]$xaml = @" <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApplication3" Title="LinkLabel Demo" Height="160" Width="329"> <Grid> <Label x:Name="Link1" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="45" FontSize='14' ToolTip='vmware'> <Hyperlink NavigateUri="http://www.vmware.com">Link1</Hyperlink> </Label> <Label x:Name="Link2" Content="Link2" HorizontalAlignment="Left" Margin="10,41,0,0" VerticalAlignment="Top" Width="45" FontSize='14' Foreground='DarkBlue' Cursor="Hand" ToolTip='Google'/> <Label x:Name="Link3" Content="Link3" HorizontalAlignment="Left" Margin="10,72,0,0" VerticalAlignment="Top" Width="45" Background='DarkGreen' Foreground='White' FontSize='14' ToolTip='vcloud-lab.com'/> </Grid> </Window> "@ #Read the form $Reader = (New-Object System.Xml.XmlNodeReader $xaml) $Form = [Windows.Markup.XamlReader]::Load($reader) #AutoFind all controls $xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]") | ForEach-Object { New-Variable -Name $_.Name -Value $Form.FindName($_.Name) -Force } #Link1 - Mouse button event $Link1.Add_PreviewMouseDown({[system.Diagnostics.Process]::start('http://www.vmware.com')}) #Link2 - My custom mouse button howver and click event $Link2.Add_MouseLeftButtonUp({[system.Diagnostics.Process]::start('http://www.google.com')}) $Link2.Add_MouseEnter({$Link2.Foreground = 'Purple'}) $Link2.Add_MouseLeave({$Link2.Foreground = 'DarkBlue'}) #Link3 - My custom mouse button howver and click event $Link3.Add_MouseLeftButtonUp({[system.Diagnostics.Process]::start('http://vcloud-lab.com')}) $Link3.Add_MouseEnter({$Link3.Background = 'DarkRed'; $Link3.Foreground = 'White'}) $Link3.Add_MouseLeave({$Link3.Background = 'DarkGreen'; $Link3.Foreground = 'White'}) #Mandetory last line of every script to load form $Form.ShowDialog() |
Userful Articles
Part 1: Create WPF XAML powershell GUI form with Visual studio
Part 2: Powershell and WPF: Build GUI applications tutorial
Part 3: Create shorter Microsoft Powershell WPF automated clean script
Powershell PoshGUI: Convert user to SID and vice versa using
Microsoft Powershell GUI: Change Internet Options connections Lan settings proxy server grayed out