Disable button in WPF?

后端 未结 5 566
一整个雨季
一整个雨季 2020-12-05 16:46

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条回答
  •  爱一瞬间的悲伤
    2020-12-05 17:53

    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:

    
       
       

    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
    

提交回复
热议问题