how to enable a button, with IsEnabled disabled

0

I'm using visual studio 2017, I tried it with IsEnabled but it does not re-enable me the button I need, that enables me and can be used with the WPF

  private void CboTipoVehiculo_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (CBOselectorVeh.SelectedIndex == 1)
        {
            btnAgregar.IsEnabled = IsEnabled = true;
        }
    }
    
asked by DonMartin 27.06.2018 в 00:21
source

3 answers

0

The IsEnabled property only accepts Boolean values. Your code does not make sense

btnAgregar.IsEnabled = IsEnabled = true;

If what you want is to enable the button according to X event I suggest you try this:

 private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
 // Un ejemplo rapido. El siguiente evento del TextBox habilitara o deshabilitara el boton si hay texto o no (Simulando un boton login)
    {
        if (textBox1.Text.Length==0)
            button1.IsEnabled = false;
        else
            button1.IsEnabled = true;

    }
    
answered by 27.06.2018 в 15:48
0

The index of the combobox goes from 0 to an, but by default the index is -1 (unless when filling it with item you say that your selectedIndex is 0), therefore if you select the first item, the index that you selected is 0.

private void CboTipoVehiculo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{   
    btnAgregar.IsEnabled = (CBOselectorVeh.SelectedIndex == 0); //true:false
}

If you want to be enabled with any item that you select whenever it is a valid item and not the one that shows by default the combobox (the null -1)

private void CboTipoVehiculo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{   
    btnAgregar.IsEnabled = (CBOselectorVeh.SelectedIndex > -1);//true:false
}

I only have one doubt that you are controlling the Combobox event CboTipoVehicle , but you are validating the SelectedIndex of CBOselectorVeh , that causes me strangeness.

Greetings.

    
answered by 27.06.2018 в 21:33
-1

With set btnAdd.IsEnabled , it should be enough to enable the button. Therefore the cause must be another.

1- Did you check with the debugger that the sentence:

btnAgregar.IsEnabled = IsEnabled = true;

is it running?

2- Verify with the debugger the final status of the IsEnabled attribute of the button, (after being suitably enabled). Most likely, it is in false.

3- If the answer to 1) is yes, then some other line of code is disabling it later. Check.

    
answered by 27.06.2018 в 02:20