Quantcast
Viewing all articles
Browse latest Browse all 18858

MVVM -How to Enable a Button on TextChanged event of a TextBox

I have this scenario:

I am using MVVM pattern.

I have a view with a Button and TextBox

Code:

<TextBox Grid.Row="0" Grid.Column="1" Width="90" Text="{Binding ProductCodeSearchString}"></TextBox>

<Button Grid.Row="5" Grid.Column="0" Width="80" Command="{Binding LookUpCommand}">Lookup</Button>

Button is mapped to a RelayCommand with respective private member & RelayCommand Property

TextBox mapped to a String with respective private member and string property

ViewModel Code like this:

string _productSearchString;

 public string ProductCodeSearchString
        {
            get
            {
                return _productCodeSearchString;
            }
            set
            {
                _productCodeSearchString = value;
                OnPropertyChanged("ProductCodeSearchString");
            }
        }

RelayCommand _lookUpCommand;

public ICommand LookUpCommand
        {
            get
            {
                if (_lookUpCommand == null)
                {
                    _lookUpCommand = new RelayCommand(
                        param => this.StartLookUp(this._productSearchString),
                        param => this.CanLookUp(this._productSearchString)
                        );
                }
                return _lookUpCommand;
            }


        }

       bool CanLookUp(string productSearchString)
        {

            return    !String.IsNullOrEmpty(productSearchString)
        }

       private void StartLookUp(string productSearchString)
        {
            //Need to implement
        }

Currently I could see the button getting enabled when something is entered in searchstring but I wanted to achieve the same when the text changed in the Textbox

What I wanted to do the Button should be enabled when something is present in the TextBox.

Also the button should be enabled when(as soon as) some text is entered in the text box

I know I need to map Text Changed event to a command and then control the enabling and disabling the BUtton.

Please help what is the way to achieve this.

Thanks

Kiran


Kiran Madiraju

Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>