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 = @"
      
      
      
      
      
      
      
      
      
      
      "@
      
      $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: 1141
      Points: 3,927
      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
  • You must be logged in to reply to this topic.