locked
XAML Textblock and Variables RRS feed

  • Question

  • Hi,

    I am coding a little private project in PowerShell and additionally need to use Xaml for a Window, where I summarise a Vocabulary test.

    I have written a test code that does nothing else, to open a window with a Table filled with buttons.

    Clear-Host
    $Test = $("Vocabulary Test Results")
    
    $AnotherTest = '<TextBlock FontWeight="Bold" Margin="10" FontFamily="Arial" FontSize="28" Foreground="Blue" HorizontalAlignment="Center" Text="$Test" />  <Separator />'
    
    #Load Assembly and Library
    Add-Type -AssemblyName PresentationFramework
    
    [xml]$Form = @"
    <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"  
    
        
    
    
        Title="MainWindow" Height="1000" Width="1000">  
    
         <ScrollViewer VerticalScrollBarVisibility="Auto">  
    
          <StackPanel VerticalAlignment="Top">  
    
           $AnotherTest 
    
          <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="1*" />
        </Grid.ColumnDefinitions>
    
        <Grid.RowDefinitions>
            <RowDefinition Height="1*" />
            <RowDefinition Height="1*" />
            <RowDefinition Height="1*" />
            <RowDefinition Height="1*" />
        </Grid.RowDefinitions>
    
        <TextBlock FontWeight="Bold" Margin="10" FontFamily="Arial" FontSize="18" Foreground="Black" HorizontalAlignment="Center" Text="Question Nr:" />
        <TextBlock Grid.Column="1" FontWeight="Bold" Margin="10" FontFamily="Arial" FontSize="18" Foreground="Black" HorizontalAlignment="Center" Text="Asked Question" />
        <TextBlock Grid.Column="2" FontWeight="Bold" Margin="10" FontFamily="Arial" FontSize="18" Foreground="Black" HorizontalAlignment="Center" Text="Expected Answer" />
        <TextBlock Grid.Column="3" FontWeight="Bold" Margin="10" FontFamily="Arial" FontSize="18" Foreground="Black" HorizontalAlignment="Center" Text="Your Answer" />
        <TextBlock Grid.Column="4" FontWeight="Bold" Margin="10" FontFamily="Arial" FontSize="18" Foreground="Black" HorizontalAlignment="Center" Text="Result" />
    
    
    
        <Button Grid.Column="1" Grid.Row="1">Button 5</Button>
        <Button Grid.Column="2" Grid.Row="1">Button 6</Button>
        <Button Grid.Row="2">Button 7</Button>
        <Button Grid.Column="1" Grid.Row="2">Button 8</Button>
    
    
    </Grid>
    
    </StackPanel>  
    </ScrollViewer>  
    </Window> 
    
    "@
    
    #Create a form
    $XMLReader = (New-Object System.Xml.XmlNodeReader $Form)
    $XMLForm = [Windows.Markup.XamlReader]::Load($XMLReader)
    
    #Show XMLform
    [void]$XMLForm.ShowDialog()

    The challenge I have is with the following line:

    $AnotherTest = '<TextBlock FontWeight="Bold" Margin="10" FontFamily="Arial" FontSize="28" Foreground="Blue" HorizontalAlignment="Center" Text="$Test" />  <Separator />'

    What I try to achieve is, to have the content for Text as a variable but for some reason, I was not able to figure out, how

    to implement the variable in such way, so that I receive the expected result. I receive error messages and also had

    suggestions to work with the backstick but that also did not work.

    Does someone have an idea, how I have to correct the code on such way, that I will get the text in the table as required.

    Thank you,

    Mike


    • Edited by M_Stgier Friday, September 27, 2019 3:26 AM
    Friday, September 27, 2019 3:24 AM

All replies

  • You don't have any textboxes. You also have no names on your controls.

    Why use Xaml. Just use Windows Forms as they are much easier for non-programmers to learn and use.

    Add-Type -AssemblyName System.Windows.Forms
    
    $form = [System.Windows.Forms.Form]::New()
    $form.StartPosition = 'CenterScreen'
    
    $textbox = [System.Windows.Forms.Textbox]::New()
    $form.Controls.Add($textbox)
    $textbox.Text = 'Change me before closing'
    
    $form.ShowDialog()
    $textbox.Text
    


    \_(ツ)_/

    Friday, September 27, 2019 3:43 AM