[WP7][MVVM Light 工具包] 使用 mvvm-light 工具包时在绑定之前过早引发按钮命令
我在 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);
}
}
}
在模拟器中,当我在文本框中输入值并单击按钮时,我可以看到我首先进入中继命令 lambda 表达式并带有断点 我看到 MyValue 为空。然后,到达 MyValue setter 中的断点,并且正确的值进入 MyValue。
我做错了什么?当然,我希望可以在 RelayCommand 之前到达设置器...
提前感谢您的帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能遇到了 TextChanged 事件的 TextBox DataBinding 问题。这是 Silverlight 3 中公认的问题,查看讨论此问题的帖子以及解决方法。一个巧妙的解决方案可能是使用 这篇文章。
HTH,indyfromoz
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