The TextChange call command does not update the text source immediately

advertisements

I am using MVVM light in my windows phone 8.1 here is code

xaml

    <TextBox Text="{Binding SearchText, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" >
        <Interactivity:Interaction.Behaviors>
            <Core:EventTriggerBehavior EventName="TextChanged">
                <Core:InvokeCommandAction Command="{Binding SearchTextChanged}"></Core:InvokeCommandAction>
            </Core:EventTriggerBehavior>
        </Interactivity:Interaction.Behaviors>
    </TextBox>

here is my VM

    private RelayCommand _searchTextChanged;

    /// <summary>
    /// Gets the SearchTextChanged.
    /// </summary>
    public RelayCommand SearchTextChanged
    {
        get
        {
            return _searchTextChanged
                ?? (_searchTextChanged = new RelayCommand(
                                      () =>
                                      {
                                          LoadContents(this.SearchText);

                                      }));
        }
    }

each time text is changed SearchTextChanged command is firing properly but the text in SearchText property is not updated it is one character less. e.g. if text in textbox is A than SearchText contains null. If text in textbox is 'aaa' than text in SearchText is only 'aa' the last character is always missing.

Any Ideas?


OK, so based on the comments, here's the answer - don't use the InvokeCommandAction but the two-way binding.

<TextBox Text="{Binding SearchText, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />

In the viewmodel behind this, there's a property called SearchText which has a setter that can call the LoadContents method, something like this...

public string SearchText
{
    get { return this.searchText; }
    set
    {
        this.searchText = value;
        NotifyPropertyChanged("SearchText");
        LoadContents(this.searchText);
    }
}

Every time the string in the TextBox changes, the setter is invoked and so is the LoadContents method.