Note that there are some explanatory texts on larger screens.

plurals
  1. POMVVM-Light: Is this the best way to validate and revert a value in the View?
    text
    copied!<p>In my View I have TextBoxes like this one:</p> <pre><code>&lt;TextBox x:Name="tBoxShippingWeight" Text="{Binding ShippingWeight, Mode=TwoWay}" InputScope="Number" /&gt; </code></pre> <p>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:</p> <pre><code>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) &gt; intValue || Convert.ToInt32(ShippingParams.ShippingWeightMax) &lt; 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); } } </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload