2

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?

Muhammad Saifullah
  • 4,292
  • 1
  • 29
  • 59
  • 1
    Why do you invoke a command on TextChanged event? You have a two-way binding set - this means that the setter of the property in your ViewModel called SearchText will be triggered every time when text is changed which means you can try invoking the command from there - it should have the full string. – Igor Ralic May 21 '14 at 12:58
  • thanks igrali, I was doing it wrong. you are right I do not need text changed event command here, two-way binding will work, Thanks :) – Muhammad Saifullah May 21 '14 at 13:08

2 Answers2

7

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.

Igor Ralic
  • 14,975
  • 4
  • 43
  • 51
0

Bumped into a very similar situation but the given workaround wasn't a solution in my case! So discovered another way to update the value.

The solution: Pass the element as parameter to the command and update the source and then call the method.

public string SearchText
{
get { return this.searchText; }
set
{
    this.searchText = value;
    NotifyPropertyChanged("SearchText");
}
}
public RelayCommand SearchTextChanged
{
    get
    {
        return _searchTextChanged
            ?? (_searchTextChanged = new RelayCommand(
                                  () =>
                                  {
                                      someCommandAction();

                                  }));
    }
}

private void someCommandAction(object obj)
{
    TextBox textBox = (obj as TextBox);
    if (textBox != null)
     {
        var be = textBox.GetBindingExpression(TextBox.TextProperty);
        if (be != null)
            be.UpdateSource();
     }
     LoadContents(textBox.Text); //textBox.text or the SearchTextproperty itself
}

XAML part:

<TextBox x:Name="textBoxName" Text="{Binding SearchText,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" >
    <Interactivity:Interaction.Behaviors>
        <Core:EventTriggerBehavior EventName="TextChanged">
            <Core:InvokeCommandAction Command="{Binding SearchTextChanged}"
                                      CommandParameter="{Binding ElementName=textBoxName}" />
        </Core:EventTriggerBehavior>
    </Interactivity:Interaction.Behaviors>
</TextBox>
Chirag Shah
  • 981
  • 1
  • 6
  • 12