Ответ 1
Это было какое-то время, и я должен был обновить этот вопрос. Я разрешил его, используя класс, который я нашел в книге WPF, написанной Яном Гриффсом (книга О'Рейли):
public static class Validator
{
/// <summary>
/// This method forces WPF to validate the child controls of the control passed in as a parameter.
/// </summary>
/// <param name="parent">Type: DependencyObject. The control which is the descendent root control to validate.</param>
/// <returns>Type: bool. The validation result</returns>
public static bool IsValid(DependencyObject parent)
{
// Validate all the bindings on the parent
bool valid = true;
LocalValueEnumerator localValues = parent.GetLocalValueEnumerator();
while (localValues.MoveNext())
{
LocalValueEntry entry = localValues.Current;
if (BindingOperations.IsDataBound(parent, entry.Property))
{
Binding binding = BindingOperations.GetBinding(parent, entry.Property);
foreach (ValidationRule rule in binding.ValidationRules)
{
ValidationResult result = rule.Validate(parent.GetValue(entry.Property), null);
if (!result.IsValid)
{
BindingExpression expression = BindingOperations.GetBindingExpression(parent, entry.Property);
Validation.MarkInvalid(expression, new ValidationError(rule, expression, result.ErrorContent, null));
valid = false;
}
}
}
}
// Validate all the bindings on the children
for (int i = 0; i != VisualTreeHelper.GetChildrenCount(parent); ++i)
{
DependencyObject child = VisualTreeHelper.GetChild(parent, i);
if (!IsValid(child))
{
valid = false;
}
}
return valid;
}
}
Тогда, на вид, я имел следующую конфигурацию:
<TextBox local:Masking.Mask="^[0-9]*$" IsEnabled="{Binding Path=LocationNumberEnabled}" Grid.Row="1" Grid.Column="3">
<Binding Path="LocationNumber" Mode="TwoWay" UpdateSourceTrigger="LostFocus" NotifyOnValidationError="True" ValidatesOnExceptions="True">
<Binding.ValidationRules>
<localValidation:PositiveNumberRule />
<localValidation:RequiredFieldRule />
</Binding.ValidationRules>
</Binding>
</TextBox>
Работал как шарм! Я просто вызвал метод IsValid каждый раз, когда я хотел вручную проверить, например. при нажатии кнопки.