60

I have a Button and a TextBox in my WPF app. How can I make the Button not enabled until the user enters some text in the TextBox?

5 Answers 5

57

This should do it:

<StackPanel> <TextBox x:Name="TheTextBox" /> <Button Content="Click Me"> <Button.Style> <Style TargetType="Button"> <Setter Property="IsEnabled" Value="True" /> <Style.Triggers> <DataTrigger Binding="{Binding Text, ElementName=TheTextBox}" Value=""> <Setter Property="IsEnabled" Value="False" /> </DataTrigger> </Style.Triggers> </Style> </Button.Style> </Button> </StackPanel> 
Sign up to request clarification or add additional context in comments.

7 Comments

It is much more elegant doing the validation in the ViewModel. Have a look at the article by Josh Smith bitbonk linked to.
@Helge Klein, I absolutely agree, but as the OP didn't specify that he was using MVVM and it isn't trivial to switch over to it if you aren't familiar, I figured I'd just answer in the simplest possible fashion.
I understand. I just wanted to point future readers to the "better" if initially more complex solution.
How Can I achieve the same for multiple textboxes and few comboxboxes but not for all of them?
Man, I have to write this amount of code just to turn the button disabled? WPF sucks!
|
30

By code:

btn_edit.IsEnabled = true; 

By XAML:

<Button Content="Edit data" Grid.Column="1" Name="btn_edit" Grid.Row="1" IsEnabled="False" /> 

Comments

30

In MVVM (wich makes a lot of things a lot easier - you should try it) you would have two properties in your ViewModel Text that is bound to your TextBox and you would have an ICommand property Apply (or similar) that is bound to the button:

<Button Command="Apply">Apply</Button> 

The ICommand interface has a Method CanExecute that is where you return true if (!string.IsNullOrWhiteSpace(this.Text). The rest is done by WPF for you (enabling/disabling, executing the actual command on click).

The linked article explains it in detail.

2 Comments

This needs more upvotes and should be marked as the answer. As @jairhumberto noted, doing it in purely XAML does completely suck. MVVM has a bit of a learning curve but as this answer says, the solution to your problem, once you're using MVVM, is phenomenally simple.
I also think this is the best answer.
5

I know this isn't as elegant as the other posts, but it's a more straightforward xaml/codebehind example of how to accomplish the same thing.

Xaml:

<StackPanel Orientation="Horizontal"> <TextBox Name="TextBox01" VerticalAlignment="Top" HorizontalAlignment="Left" Width="70" /> <Button Name="Button01" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10,0,0,0" /> </StackPanel> 

CodeBehind:

Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded Button01.IsEnabled = False Button01.Content = "I am Disabled" End Sub Private Sub TextBox01_TextChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles TextBox01.TextChanged If TextBox01.Text.Trim.Length > 0 Then Button01.IsEnabled = True Button01.Content = "I am Enabled" Else Button01.IsEnabled = False Button01.Content = "I am Disabled" End If End Sub 

1 Comment

It windows 8 beta it is going to be the only possibility to achieve this. At least for today.
2

You could subscribe to the TextChanged event on the TextBox and if the text is empty set the Button to disabled. Or you could bind the Button.IsEnabled property to the TextBox.Text property and use a converter that returns true if there is any text and false otherwise.

1 Comment

I was gonna write "it's usefull in 2023" but I have noticed the post is in 2010! I think it's interesting that even though this is the definitive correct answer to the question, nobody cares since 2010!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.