禁用 WPF 中的按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2906346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:48:14  来源:igfitidea点击:

Disable button in WPF?

wpfbuttontextbox

提问by sanjeev40084

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?

我的 WPF 应用程序中有一个 Button 和一个 TextBox。在用户在 TextBox 中输入一些文本之前,如何使 Button 不启用?

回答by Steve Greatrex

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>

回答by bitbonk

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

MVVM 中(这让很多事情变得更容易——你应该尝试一下)你的 ViewModelText中有两个属性绑定到你的 TextBox,你会有一个 ICommand 属性Apply(或类似的)绑定到按钮:

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

The ICommandinterface has a Method CanExecutethat is where you return trueif (!string.IsNullOrWhiteSpace(this.Text). The rest is done by WPF for you (enabling/disabling, executing the actual command on click).

ICommand接口有一个 Method CanExecute,它是您返回的地方trueif (!string.IsNullOrWhiteSpace(this.Text)。其余的由 WPF 为您完成(启用/禁用,单击时执行实际命令)。

The linked article explains it in detail.

链接的文章详细解释了它。

回答by daniele3004

By code:

按代码:

btn_edit.IsEnabled = true;

By XAML:

通过 XAML:

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

回答by spong

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 示例,说明如何完成相同的事情。

Xaml:

Xml:

<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

回答by Charlie

You could subscribe to the TextChangedevent on the TextBoxand if the text is empty set the Buttonto disabled. Or you could bind the Button.IsEnabledproperty to the TextBox.Textproperty and use a converter that returns true if there is any text and false otherwise.

您可以订阅TextChanged事件TextBox,如果文本为空,则将 设置Button为禁用。或者,您可以将该Button.IsEnabled属性绑定到该TextBox.Text属性,并使用一个转换器,如果有任何文本则返回 true,否则返回 false。