Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I have an XAML interface and I am trying to uncheck a box and change its foreground color from one color to another. Unfortunately, it is not working as planned. Even though I have both commands running within the Add_Click event, I have to click twice, one-click to uncheck, and then another click to change its color, but it's not working as a whole. I've even tried running it from a function and it's still a recurring issue.

What I have tried:

When checked if $DC shows a lock
PowerShell
If($UserInfo.LastBadPasswordAttempt){
    if($UserInfo.LockedOut -eq $true) {
        if($DC -match $_chkServer1.Content) {
            $_chkServer1.IsChecked = $true
            $_chkServer1.Foreground = 'Red'
        }
    }        
}

Then when users uncheck with the Add_Click({}) event.
PowerShell
$_chkServer1.Add_Click({
    if($_chkServer1.IsChecked)
    {
        $_chkServer1.IsChecked = $false             
        $_chkServer1.Foreground = 'Green'
    }
})

And then with a function
PowerShell
$_chkServer1.Add_Click({
    if($_chkServer1.IsChecked)
    {
        ChangeState -i 1
    }
}) 

Function ChangeState([int]$i)
{
    switch ($i)
    {
        1 {
            $_chkServer1.IsChecked = $false             
            $_chkServer1.Foreground 
        }
    }
}

The foreground does not change with the uncheck. When I uncheck, the foreground remains the same (red) then I click again (on the content of the checkbox), then it changes to green. What the result should be: When I uncheck the box, it should automatically switch green, but it does not do this, I have the click it twice for it to turn green.
Posted
Updated 4-Sep-20 3:27am
v2

1 solution

If the box is checked and the user clicks on it, it will be unchecked before the Click event is raised.

So the first time you click, the box is unchecked, and your event handler does nothing. The second time you click, the box is checked, and your event handler unchecks it again.

Change your event handler to:
PowerShell
$_chkServer1.Add_Click({
    if (-Not $_chkServer1.IsChecked)
    {
        $_chkServer1.Foreground = 'Green'
    }
})
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900