繁体   English   中英

[WP7] [MVVM Light工具包]在使用mvvm-light工具包进行绑定之前,按钮命令提起得太早

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

我在Windows Phone 7应用程序中使用了mvvm-light工具包。

我认为:

<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}"  />

我的视图模型是:

    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);
        }
    }
}

在仿真器中,当我在文本框中键入value并单击按钮时,可以看到我首先在relaycommand lambda表达式中执行,并且在断点处看到MyValue为null。 然后,到达MyValue的设置器中的断点,并且正确的值进入MyValue。

我究竟做错了什么 ? 当然,我希望可以在RelayCommand之前到达setter。

在此先感谢您的帮助。

您可能已经通过TextChanged事件遇到了TextBox DataBinding问题。 这是Silverlight 3中公认的问题, 请参阅此线程讨论此问题和解决方法。 一个整洁的解决方案,或许是使用行为作为讨论这篇文章

HTH,indyfromoz

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM