开发者

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

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 th开发者_如何学编程at 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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜