Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

disable/enable a button depending on textbox in xaml

I have a TextBox

<TextBox x:Name="searchTextBox" Width="200" Height="30" HorizontalAlignment="Left"/>

and two Buttons

<Button x:Name="previous" Style="{StaticResource AppBarButtonStyle}" Tapped="OnOptionItemTapped" IsEnabled="False">
    <Image Source="/Assets/images/left_arrow.png"/>
</Button>
    <Button x:Name="next" Style="{StaticResource AppBarButtonStyle}"  Tapped="OnOptionItemTapped" IsEnabled="False">
    <Image Source="/Assets/images/right_arrow.png"/>
</Button>

Is there a simple solution to enable/disable the Buttons trough the TextBox?

Like for example if the TextBox is empty the Buttons are disabled. And if the TextBox is not empty the Buttons are enabled.

like image 342
axbeit Avatar asked Dec 03 '25 09:12

axbeit


1 Answers

You could apply a text changed event that checks the input every time it changes.

<TextBox x:Name="searchTextBox" Width="200" Height="30" HorizontalAlignment="Left" TextChanged="TextBox_TextChanged" />

If the text is the way you need it, you can turn the button enabled/disabled.

public void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
    if (searchTextBox.Text == result)
        next.IsEnabled = false;
}

EDIT: Other than code behind like this approach you could learn about the MVVM design pattern. The other answers partly used practices common in MVVM.

There are various good tutorials for this all around the web.

like image 188
Faenrig Avatar answered Dec 05 '25 00:12

Faenrig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!