如何使一个按钮,当用户键入到一个文本框 [英] How to enable a button when a user types into a textbox

查看:92
本文介绍了如何使一个按钮,当用户键入到一个文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是WPF最简单的方法,以使按钮当用户键入的东西到文本框 <? / p>

解决方案

使用了简单的命令

 &LT;文本框的文本= {绑定路径= TitleText} /&GT;&LT;按钮命令={绑定路径= ClearTextCommand}CONTENT =明文/&GT;

下面是视图模型样本code

 公共类MyViewModel:INotifyPropertyChanged的
{
    公众的ICommand ClearTextCommand {搞定;私人集; }    私人字符串_titleText;
    公共字符串TitleText
    {
        {返回_titleText; }
        组
        {
            如果(价值== _titleText)
                返回;            _titleText =价值;
            this.OnPropertyChanged(TitleText);
        }
    }    公共MyViewModel()
    {
        ClearTextCommand =新SimpleCommand
            {
                ExecuteDelegate = X =&GT; TitleText =,
                CanExecuteDelegate = X =&GT; TitleText.Length&GT; 0
            };
    }    公共事件PropertyChangedEventHandler的PropertyChanged;    保护无效OnPropertyChanged(字符串propertyName的)
    {
        PropertyChangedEventHandler处理器= this.PropertyChanged;
        如果(处理!= NULL)
            处理器(这一点,新PropertyChangedEventArgs(propertyName的));
    }
}

有关更多信息,请参阅马龙Grechs <一个href=\"http://marlongrech.word$p$pss.com/2008/11/26/avoiding-commandbinding-in-the-xaml-$c$c-behind-files/\"相对=nofollow> SimpleCommand

还检查了从的http://blogs.msdn.com/llobo/archive/2009/05/01/download-m-v-vm-project-template-toolkit.aspx.它使用DelegateCommand的指挥,它应该是任何项目的理想出发模板。

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

解决方案

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

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.

这篇关于如何使一个按钮,当用户键入到一个文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆