简体   繁体   中英

Enable a disabled button in WPF

So, I have a button that 's disabled by default on form load and I'd like to enable it when all conditions evaluate to TRUE.

private void OnFormLoad(object sender, RoutedEventArgs e)
    {
        BtnInvoice.IsEnabled = false;
    }
 private void Btn_Click(object sender, RoutedEventArgs e)
    {
        if (Regex.IsMatch(A.Text, "^[a-zA-Z' ]*$") && Regex.IsMatch(B.Text, "^[0-9.]*$") && Regex.IsMatch(C.Text, "^[0-9.]*$"))
        {
         BtnInvoice.IsEnabled = true;
        }

In the above code, when all conditions evaluate to TRUE, my button remains disabled. I'm pretty sure I put the enabling block in the wrong place may also have a logic error.

In situations that I must determine when a button needs to be enabled or not, I prefer implement a Command on that button. The CanExecute on the command holds the logic whether the button is actually enabled or not.

See http://www.wpf-tutorial.com/commands/using-commands .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM