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

    Question

  • 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
    Wednesday, November 24, 2010 3:32 PM

Answers

  • Hi Kiran,

    Just updating the text box binding with PropertyChanged for UpdateSourceTrigger should fix this.

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

    Thanks,
    Muhammad


    Muhammad Siddiqi
    • Marked as answer by Kiran75 Wednesday, November 24, 2010 4:03 PM
    Wednesday, November 24, 2010 3:45 PM

All replies

  • you could check if TextBox has text in your CanLookup implementation
    konstantin
    Wednesday, November 24, 2010 3:39 PM
  • Yes I am doing that:

    !String.IsNullOrEmpty(productSearchString)


    Kiran Madiraju
    Wednesday, November 24, 2010 3:40 PM
  • Hi Kiran,

    Just updating the text box binding with PropertyChanged for UpdateSourceTrigger should fix this.

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

    Thanks,
    Muhammad


    Muhammad Siddiqi
    • Marked as answer by Kiran75 Wednesday, November 24, 2010 4:03 PM
    Wednesday, November 24, 2010 3:45 PM
  • The most efficient solution to is to use/develop the XAML -markup extension where you can solve this by delegating create instance of the “relay” to the XAML markup extension.

    Here are links:

    Saturday, May 18, 2013 5:56 PM