XAML : Change button text depending on a variable

Welcome Forums General PowerShell Q&A XAML : Change button text depending on a variable

Viewing 1 reply thread
  • Author
    Posts
    • #184619
      Participant
      Topics: 2
      Replies: 0
      Points: 12
      Rank: Member

      Hey guys,
      A PowerShell XAML question: How can you change a button ‘s ‘Content’ after it has been pressed?
      I’m loading a Window from where I can search for a user and enable or disable that user.
      Depending on the initial “status” of the user I’d like to change the text on the button to show the action to perform
      e.g.: If the searched user is disabled, the text on the button has to change to ‘enable’ – and vice versa

      As I’m very new to building a GUI with XAML, I have no idea how to do this in PowerShell.

      Example of the code below.

       

      function SearchUser
      {
      $global:user = Get-ADUser $SearchText.Text
      }
      
      function Enable-Disable
      {
      if (($user.Enabled).ToString() -eq "True")
      {
      # {Change Content EnableDisableButton to 'Disable'}
      # Disable-ADAccount $user
      }
      
      if (($user.Enabled).ToString() -eq "False")
      {
      # {Change Content of EnableDisableButton to 'Enable'}
      # Enable-ADAccount $user
      }
      }
      
      Add-Type -AssemblyName PresentationFramework
      
      [xml]$xaml = @"
      <Window Title="Test" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="Window">
      <Grid>
      <Label HorizontalAlignment="Left" VerticalAlignment="Top" Name="SearchLabel" Content="Search (sAMAccountName):" Width="300" Height="25" Margin="10,10,0,0" />
      <TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Name="SearchTextBox" Width="300" Height="30" Margin="10,35,0,0" />
      <Button HorizontalAlignment="Left" VerticalAlignment="Top" Name="SearchButton" Content="SEARCH" Width="300" Height="50" Margin="10,75,0,0" />
      
      <Label HorizontalAlignment="Left" VerticalAlignment="Top" Name="EnableDisableLabel" Content="Enable or Disable a User:" Width="300" Height="25" Margin="10,175,0,0" />
      <Button HorizontalAlignment="Left" VerticalAlignment="Top" Name="EnableDisableButton" Content="ENABLE OR DISABLE" Width="300" Height="50" Margin="10,200,0,0" />
      </Grid>
      </Window>
      "@
      
      $reader = (New-Object System.Xml.XmlNodeReader $xaml)
      $window = [Windows.Markup.XamlReader]::Load($reader)
      
      $global:SearchText = $window.FindName("SearchTextBox")
      
      $Search = $window.FindName("SearchButton")
      $Search.Add_Click({SearchUser})
      
      $EnableDisable = $window.FindName("EnableDisableButton")
      $EnableDisable.Add_Click({EnableDisable})
      
      $window.ShowDialog()
    • #187504
      Senior Moderator
      Topics: 8
      Replies: 1215
      Points: 4,335
      Helping Hand
      Rank: Community Hero

      try without using global variables and functions.

      $window = [Windows.Markup.XamlReader]::Load($reader)
      
      $Search= $window.FindName("SearchButton")
      $Search.Text = 'Disabled'
      if($user.Enabled){
          $Search.Text = 'Disabled'
      }
      #....
      
Viewing 1 reply thread
  • The topic ‘XAML : Change button text depending on a variable’ is closed to new replies.