我正在尝试使用验证规则验证WPF / MVVM中的组合框选定项目,但无法使其工作。我有这个XAML:
<ComboBox Grid.Row="2" Margin="0,0,0,5" Grid.Column="1"
ItemsSource="{Binding Tiers}" Validation.ErrorTemplate="{StaticResource DefaultErrorTemplate}"
DisplayMemberPath="Description"
SelectedValuePath="Id">
<ComboBox.SelectedItem>
<Binding Path="SelectedSchoolProgramTierCombined.TierId" ValidatesOnDataErrors="True"
Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<validationRules:RequiredValidationRule/>
</Binding.ValidationRules>
</Binding>
</ComboBox.SelectedItem>
我有一个SelectedSchoolProgramTierCombined属性设置器并进入我的ViewModel。
我的验证规则是:
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
if (value == null || object.Equals(value, string.Empty))
return new ValidationResult(false, "The field is mandatory.");
else
return ValidationResult.ValidResult;
}
任何想法都会受到赞赏。