[WP7][MVVM Light toolkit] Button Command raised too early, before binding when using mvvm-light toolkit

StackOverflow https://stackoverflow.com/questions/4012450

  •  26-09-2019
  •  | 
  •  

Question

I'm using the mvvm-light toolkit in my windows phone 7 application.

I have in my view :

<TextBox Height="78" HorizontalAlignment="Left" Margin="108,33,0,0" VerticalAlignment="Top" Width="313" Text="{Binding MyValue, Mode=TwoWay}" />
<Button Content="Go" Height="78" HorizontalAlignment="Left" Margin="127,252,0,0" Name="button1" VerticalAlignment="Top" Width="213" cmd:ButtonBaseExtensions.Command="{Binding DoCommand}"  />

My view model is :

    public class MainPageViewModel : ViewModelBase
    {
        public ICommand DoCommand { get; internal set; }
    public MainPageViewModel()
    {
        DoCommand = new RelayCommand(() =>
            {
                DoSomethingWith(MyValue);
            }, () => true);

    }

    private const string MyValuePropertyName = "MyValue";
    private string _myValue;
    public string MyValue
    {
        get { return _myValue; }
        set
        {
            if (_myValue == value)
                return;
            _myValue = value;
            RaisePropertyChanged(MyValuePropertyName);
        }
    }
}

In the emulator, when I type value in the textbox, and I click the button, I can see that I'm going first in the relaycommand lambda expression and with a breakpoint I see that MyValue is null. Then, the breakpoint in the setter of MyValue is reached, and the correct value goes in MyValue.

What am I doing wrong ? Of course, I would like that the setter can be reached before the RelayCommand...

Thanks in advance for any help.

Was it helpful?

Solution

It is possible that you have hit the TextBox DataBinding issue with the TextChanged event. This is a recognized problem in Silverlight 3, see this thread discussing this issue and the workaround. A neat solution is perhaps to use behaviors as discussed in this article.

HTH, indyfromoz

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top