disable/enable a button depending on textbox in xaml

enable/disable button in wpf mvvm
wpf button isenabled=(binding)
wpf disable button programmatically
wpf button enabled binding
wpf enable button when textbox is not empty
wpf disable button on validation error
xamarin disable button after click
wpf enable button based on multiple textbox

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 x:Name="next" Style="{StaticResource AppBarButtonStyle}"  Tapped="OnOptionItemTapped" IsEnabled="False">
    <Image Source="/Assets/images/right_arrow.png"/>

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.

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.

How to make Button enabled/disabled depending on the TextBox , Keep button inactive until a TextBox has a value, using WPF. Sometimes we need to enable/disable controls based on a property of another  Sometimes we need to enable/disable controls based on a property of another control. Like if a textbox has some value, only then enable the button, else disable. In the example below, I have a textbox txtName and once the user enters something (as soon as) into the textbox, then enable the “Add Name” button. Way 1. Way 2.

How about using binding + converter? I guess this concept is valid for UWP as well.

E.g. you have a view model with property SearchText which is bound to the text in the TextBox. Then you can do the following:

    <local:StringToBoolConverter x:Key="StringToBoolConverter" />


<TextBox x:Name="searchTextBox" Width="200" Height="30" HorizontalAlignment="Left" Text="{Binding SearchText}"/>
<Button x:Name="previous" IsEnabled="{Binding SearchText, Converter={StaticResource StringToBoolConverter}}"/>

And the converter code would be quite simple:

public class StringToBoolConverter : IValueConverter
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        return !string.IsNullOrEmpty(value?.ToString());

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        throw new NotImplementedException();

Another way to go is using Command pattern for the buttons. The ICommand interface has CanExecute method that will cantually disable or enable your buttons depending on the return value. See examples in the internet or here.

disable/enable a button depending on textbox in xaml, disable/enable a button depending on textbox in xaml. Multi tool Is there a simple solution to enable/disable the Buttons trough the TextBox? I was looking for code through google that enables and disables a button based on the text property from a textbox. If the textbox has some text then the button should be enabled or otherwise button should be disabled. I got some code but code in xaml but no c#. I am not being able to understand how it works.

Use binding for IsEnabled.

<Button x:Name="previous" Style="{StaticResource AppBarButtonStyle}" 
 Tapped="OnOptionItemTapped" IsEnabled="{Binding ElementName=searchTextBox, 
Path=Text.Length, Mode=OneWay}"></Button>

You can also use Data Triggers but the above is simplest. Converters are not required.

WPF IsEnabled Property (Button Example), So: The Button becomes enabled when the TextBox has text. It remains disabled whenever the TextBox is empty. Example markup: XAML <Window  When the text from the textbox is removed then the button will be disabled. Step 6: Run the application and you should see the following result with ‘Get Persons’ button as disabled: Enter some text in the TextBox and the Get Person button will be enabled. Click on the button and the result will be as shown here:

Control.Enabled Property (System.Windows.Forms), The example creates a TextBox and sets its Location within the group box. C# Copy. // Add a GroupBox to a form and set some of its common properties. private void With the Enabled property, you can enable or disable controls at run time. For example, a button can be disabled to prevent the user from clicking it. Enable/disable textbox based on checkbox selection in WPF using MVVM. I have a WPF form with as many as 40 textboxes, with a checkbox for each.

Control.IsEnabled Property (Windows.UI.Xaml.Controls), For example, if a control that contains a button has IsEnabled set to false, the However, a disabled control can still source the input events, and input routed  Enable or Disable a Control with a CheckBox Using Data Binding By Michael Detras There are some times when we enable or disable some controls depending on whether a CheckBox in a form is checked or not.

WPF Commanding: Enable Disable Button with Command Property , Walkthrough for using WPF commanding for enabling / disabling Button The command property is available for action based elements for e.g. Button, text in the Search textbox, the Search button should become enabled. Depending on the initial “status” of the user I’d like to change the text on the button to show the action to perform e.g.: If the searched user is disabled, the text on the button has to change to ‘enable’ – and vice versa. As I’m very new to building a GUI with XAML, I have no idea how to do this in PowerShell. Example of the code below.

  • Usage of event handlers should be discouraged in WPF
  • I can't comply with that unless the question stated a design pattern like MVVM was used. I would agree with you otherwise as I don't like code behind that much myself. A simple approach was asked and I believe mine was pretty simple.
  • When you code WPF, MVVM should be the default thought, Particularly styling and UI related stuff should be handled within XAML as long as sustainable. You are answer is perfectly alright, its just about encouraging the right behaviour for new programmers.
  • This is an ambiguous topic to me. A programmer should always try to keep it simple and try things out for himself. So if something is ugly, but it works, it's fine for the programmer's solution. This is at least a very good way to learn how a program works. I can edit my answer though to do as you say, encourage newer developers to learn a widely accepted design pattern for WPF.