MVVM-Light:这是验证和恢复视图中的值的最佳方法吗?
在我的视图中,我有这样的文本框:
<TextBox x:Name="tBoxShippingWeight" Text="{Binding ShippingWeight, Mode=TwoWay}" InputScope="Number" />
当用户输入一个值时,我会进行基本验证(不是这个问题的重点),如果失败,我使用 System.Windows.MessageBox 通知用户正确的参数和将值恢复为之前的值,该值始终有效,因为它要么来自默认初始化值,要么由用户正确输入。我最终在执行验证的 if 语句中调用 RaisePropertyChanged("ShippingWeight") 。如果我把它放在 catch 语句中,那么如果也从那里调用 MessageBox,它就永远不会被引发。这是进行恢复的合理方法还是有更好的方法?这是 ViewModel 中的代码:
public string ShippingWeight
{
get { return ShippingModel.ShippingWeight.ToString(); }
set
{
if (ShippingModel.ShippingWeight.ToString() == value) return;
var oldValue = ShippingModel.ShippingWeight;
try
{
int intValue = Convert.ToInt32(value);
if (Convert.ToInt32(ShippingParams.ShippingWeightMin) > intValue || Convert.ToInt32(ShippingParams.ShippingWeightMax) < intValue)
{
// Revert back to previous value
// NOTE: This has to be done here. If done in the catch statement,
// it will never run since the MessageBox interferes with it.
RaisePropertyChanged("ShippingWeight");
throw new Exception();
}
ShippingModel.ShippingWeight = intValue;
RaisePropertyChanged("ShippingWeight", oldValue, Convert.ToDouble(value), true);
}
catch (Exception)
{
System.Windows.MessageBox.Show("Value must be a whole number between " + ShippingParams.ShippingWeightMin + " and " + ShippingParams.ShippingWeightMax);
}
}
}
In my View I have TextBoxes like this one:
<TextBox x:Name="tBoxShippingWeight" Text="{Binding ShippingWeight, Mode=TwoWay}" InputScope="Number" />
When the user enters a value I do a basic validation (not the focus of this question) and if that fails I use the System.Windows.MessageBox to notify the user of the proper params and revert the value back to the previous value, which is always valid because it either came from a default init value or was properly entered by the user. I ended up calling RaisePropertyChanged("ShippingWeight") inside the if statement that does the validation. If I put it in the catch statement, it never gets raised if the MessageBox is also called from there. Is this a reasonable way to do the reversion or is there something better? Here is the code from the ViewModel:
public string ShippingWeight
{
get { return ShippingModel.ShippingWeight.ToString(); }
set
{
if (ShippingModel.ShippingWeight.ToString() == value) return;
var oldValue = ShippingModel.ShippingWeight;
try
{
int intValue = Convert.ToInt32(value);
if (Convert.ToInt32(ShippingParams.ShippingWeightMin) > intValue || Convert.ToInt32(ShippingParams.ShippingWeightMax) < intValue)
{
// Revert back to previous value
// NOTE: This has to be done here. If done in the catch statement,
// it will never run since the MessageBox interferes with it.
RaisePropertyChanged("ShippingWeight");
throw new Exception();
}
ShippingModel.ShippingWeight = intValue;
RaisePropertyChanged("ShippingWeight", oldValue, Convert.ToDouble(value), true);
}
catch (Exception)
{
System.Windows.MessageBox.Show("Value must be a whole number between " + ShippingParams.ShippingWeightMin + " and " + ShippingParams.ShippingWeightMax);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以考虑实现 IEditableObject
使用一些私有字段当调用 BeginEdit 时存储旧值,并在验证失败或用户取消编辑时使用这些值。
You could consider to implement IEditableObject
Use some private fields to store the old values when BeginEdit is called and use these values when validation fails or the user cancels the edit.