Enable WPF C# Button through a function

Asked

Viewed 221 times

1

I have a form to fill in some data through Textbox.

I have a class that has some validation functions of fields name, password, user for example. And in my Code Behind just call the function that validates by passing the parameters to it. If each input field is valid a string declared receives the value of okay.

I would like that at the end after all fields are validated the save button that is disabled was enabled.

Look how I’m doing:

string validacaoImg = "";
private void txt_nome(object sender, TextChangedEventArgs e)
{
    string regex = "^[A-Za-záéíóúàâêôãõüçÁÉÍÓÚÀÂÊÔÃÕÜÇ ]+$";
    Funcao.validaCampos(regex, txtNome.Text, imgNome, validacaoImg);
}

private void txt_dtNascimento(object sender, SelectionChangedEventArgs e)
{
    string regex = @"\d{2,2}/\d{2,2}/\d{4,4}";
    Funcao.validaCampos(regex, txtNascimento.Text, imgNascimento, validacaoImg);
}

private void txt_usuario(object sender, TextChangedEventArgs e)
{
    string regex = "^[[email protected]]+$";
    Funcao.validaCampos(regex, txtUsuario.Text, imgUsuario, validacaoImg);
}

private void txt_senha(object sender, TextChangedEventArgs e)
{
    string regex = @"(?=.*[A-Z])(?=.*[a-z])(?=.*\d)[A-Za-z0-9].+$";
    Funcao.validaCampos(regex, txtSenha.Text, imgSenha, validacaoImg);
}

My class Função:

public static void validaCampos(string regex, string txt, Image img, string validacaoImg)
{
    TextBox texto = new TextBox();
    texto.Text = txt;
    img.Visibility = Visibility.Visible;
    Regex rx = new Regex(regex);
    if (texto.Text.Length > 0)
    {
        if (validaTextBoxes(txt, rx))
        {                        
            texto.BorderBrush = Brushes.Green;
            validacaoImg = "check";
            texto.ToolTip = null;
        }
        else
        {
            texto.BorderBrush = Brushes.Red;                    
            texto.ToolTip = "Fora do padrão!";
            validacaoImg = "errado";
        }
    }
    else
    {
        texto.BorderBrush = Brushes.Red;
        texto.ToolTip = "Este campo não pode ficar vazio!";
        validacaoImg = "errado";                
    }
    validarImagem(img, validacaoImg);
}

public static bool validaTextBoxes(string texto, Regex regex)
{
    bool isValid = regex.IsMatch(texto);

    if (isValid)
    {
        return true;
    }
    else
    {
        return false;
    }
}

public static void validarImagem(Image img, string validacaoImg)
{
    var path = System.IO.Path.Combine(Environment.CurrentDirectory, "Imagens/");
    var uri = new Uri(path + validacaoImg + ".jpg");
    BitmapImage bm = new BitmapImage(uri);
    img.Source = bm;
}

inserir a descrição da imagem aqui

  • The first block of code is called when? No Leave?

  • @Jéfersonbueno you refer to which block of the Class I created Funcao or what is in the XAML Codebehind?

  • I mean the functions txt_nome, txt_dtNascimento and subsequent.

  • @Jéfersonbueno yes

1 answer

0

Add the Controls within System.Windows.Controls.Validationerror, and with each validation you take them out(validated), or keep them (not validated) and check them in the Post’s. a Collection Reaching zero you Enable the save.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.