MVVM-Light:这是验证和恢复视图中的值的最佳方法吗?

发布于 2024-10-16 21:44:53 字数 1547 浏览 3 评论 0原文

在我的视图中,我有这样的文本框:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〃温暖了心ぐ 2024-10-23 21:44:53

您可以考虑实现 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文