wpf 如何在用户输入文本框时启用按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/821121/
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 20:22:10  来源:igfitidea点击:

How to enable a button when a user types into a textbox

wpfbutton

提问by user253175

What is the simplest way in WPF to enable a Buttonwhen the user types something into a TextBox?

在 WPF 中启用 aButton当用户在 a 中键入内容时最简单的方法是TextBox什么?

采纳答案by Soni Ali

Use the Simple Command

使用简单命令

<TextBox Text={Binding Path=TitleText}/>

<Button Command="{Binding Path=ClearTextCommand}" Content="Clear Text"/>

Here is the sample code in the View Model

这是视图模型中的示例代码

public class MyViewModel : INotifyPropertyChanged
{
    public ICommand ClearTextCommand { get; private set; }

    private string _titleText; 
    public string TitleText
    {
        get { return _titleText; }
        set
        {
            if (value == _titleText)
                return;

            _titleText = value;
            this.OnPropertyChanged("TitleText");
        }
    }   

    public MyViewModel()
    {
        ClearTextCommand = new SimpleCommand
            {
                ExecuteDelegate = x => TitleText="",
                CanExecuteDelegate = x => TitleText.Length > 0
            };  
    }            

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }     
}

For more information see Marlon Grechs SimpleCommand

有关更多信息,请参阅 Marlon Grechs SimpleCommand

Also check out the MVVM Project Template/Toolkit from http://blogs.msdn.com/llobo/archive/2009/05/01/download-m-v-vm-project-template-toolkit.aspx. It uses the DelegateCommand for commanding and it should be a great starting template for any project.

还可以从http://blogs.msdn.com/llobo/archive/2009/05/01/download-mv-vm-project-template-toolkit.aspx查看 MVVM 项目模板/工具包。它使用 DelegateCommand 进行命令,它应该是任何项目的一个很好的起始模板。

回答by user253175

Why is everyone making things so complicated!

为什么大家都把事情搞得这么复杂!

    <TextBox x:Name="TB"/>
    <Button IsEnabled="{Binding ElementName=TB,Path=Text.Length}">Test</Button>

Nothing else needed......

别的什么都不需要......

回答by Bert

Use a trigger!

使用触发器!

<TextBox x:Name="txt_Titel />
<Button Content="Transfer" d:IsLocked="True">
  <Button.Style>
    <Style>
      <Style.Triggers>
        <DataTrigger Binding="{Binding ElementName=txt_Titel, Path=Text}" Value="">
         <Setter Property="Button.IsEnabled" Value="false"/>
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </Button.Style>
</Button>

回答by Dennis

IF you were not using Commands, another alternative is using a Converter.

如果您没有使用命令,另一种选择是使用转换器。

For example, using a generic Int to Bool converter:

例如,使用通用的 Int 到 Bool 转换器:

  [ValueConversion(typeof(int), typeof(bool))]
  public class IntToBoolConverter : IValueConverter
  {
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
      try
      {
        return (System.Convert.ToInt32(value) > 0);
      }
      catch (InvalidCastException)
      {
        return DependencyProperty.UnsetValue;
      }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
      return System.Convert.ToBoolean(value) ? 1 : 0;
    }

    #endregion
  }

Then on the buttons IsEnabled property:

然后在按钮 IsEnabled 属性上:

<Button IsEnabled={Binding ElementName=TextBoxName, Path=Text.Length, Converter={StaticResource IntToBoolConverter}}/>

HTH,

哈,

Dennis

丹尼斯

回答by George30

The key on this is on the binding itself..

关键在于绑定本身..

Add UpdateSourceTrigger = PropertyChanged

添加 UpdateSourceTrigger = PropertyChanged

this is the most simple solution

这是最简单的解决方案

回答by Anzurio

Add a callback to the TextBox that fires on every stroke. Test for emptiness in such callback and enable/disable the button.

将回调添加到在每个笔画上触发的 TextBox。在此类回调中测试是否为空并启用/禁用按钮。