0
我有一個文本框與莫綁定屬性:WPF MVVMLight二傳手上綁定文本框不會觸發時驗證規則是無效的
<TextBox>
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Setters>
<Setter Property="Text">
<Setter.Value>
<Binding Path="Mo" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<validator:FloatPositiveValidationRule ValidatesOnTargetUpdated="True" />
</Binding.ValidationRules>
</Binding>
</Setter.Value>
</Setter>
</Style.Setters>
</Style>
</TextBox.Style>
</TextBox>
這其中包含了驗證僅當這個值是控制驗證規則不是空的和大於0:
public class FloatPositiveValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
string str = (string)value;
if (str.Length > 0)
{
double mo = Double.Parse(str.Replace(".", ","));
if (mo > 0)
return new ValidationResult(true, null);
else
return new ValidationResult(false, "Must be greater than 0");
}
else
return new ValidationResult(false, "Empty");
}
}
在我看來模型中,當確認是假的,二傳手是不會觸發:
private double? _mo;
public string Mo
{
get { return _mo.ToString(); }
set
{
if (value != "")
mo = double.Parse(value.Replace(".", ","));
Set(ref _mo, mo);
}
}
即使驗證無效,是否可以在setter中輸入?