Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 8583

XAML Frontend and keyevents

$
0
0

Hi,

I am trying to add the "Enter" keyevent to a textbox that was created with XAML. I found several code examples for C# and other languages but none for powershell.

Here is a little code example to show you what I mean. I want to enter some text in the input box and when I press the enter key the get-data function shall be executed.

################################

function get-data {
 if ($intxt.text -eq "") {
  $Outtxt.text = "nothing"
 }
 else {
  $Outtxt.text = $Intxt.text
 }
}
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$XAML = @'
<Window Name="Test"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBox Name="Intxt" HorizontalAlignment="Left" Height="23" Margin="317,32,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="105,32,0,0" TextWrapping="Wrap" Text="Input:" VerticalAlignment="Top" IsReadOnly="True" Width="120" Background="#FFA7CCF7"/>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="105,100,0,0" TextWrapping="Wrap" Text="Output:" VerticalAlignment="Top" IsReadOnly="True" Width="120" Background="#FFA7CCF7"/>
        <TextBox Name="Outtxt" HorizontalAlignment="Left" Height="23" Margin="317,100,0,0" TextWrapping="Wrap" VerticalAlignment="Top" IsReadOnly="True" Width="120" Background="#FFC4F7A7"/>
        <Button Name="DoIt" Content="Do it" HorizontalAlignment="Left" Margin="211,208,0,0" VerticalAlignment="Top" Width="75"/>

    </Grid>
</Window>
'@

#Read XAML
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
catch{Write-Host "Unable to load Windows.Markup.XamlReader. Some possible causes for this problem include: .NET Framework is missing PowerShell must be launched with PowerShell -sta, invalid XAML code was encountered."; exit}

 
$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name ($_.Name) -Value $Form.FindName($_.Name)}
 
#Button

$DoIt.Add_Click({get-data})

$Form.ShowDialog() | out-null

###################################

Any ideas?

Best regrds

Sven


Viewing all articles
Browse latest Browse all 8583

Trending Articles