ComboBox에 대한 자체 유효성 검사 규칙을 작성하려고했으나 해당 규칙이 SelectedItem에 대한 바인딩에 연결되어 있습니다. 그러나 작동하지 않습니다. 나는 바인딩에 대한 WPF 유효성 검사 - ComboBox SelectedItem이 유효성을 검사하지 않습니다.
<ComboBox VerticalAlignment="Top" ItemsSource="{Binding Animals}" DisplayMemberPath="Name" >
<ComboBox.SelectedItem>
<Binding Path="Animal">
<Binding.ValidationRules>
<validators:ComboBoxValidationRule ErrorMessage="Please select an animal" />
</Binding.ValidationRules>
</Binding>
</ComboBox.SelectedItem>
</ComboBox>
내가 코드를 그 아래는 제가 인터넷에서 찾은 확인 전화를 사용하고 있습니다 생각 ... Text 속성 작업 유사한 규칙을 가지고있다. 기본적으로 SelectedItem 종속성 속성을오고 있지 않습니다.
TextProperty 및 SelectionBoxItemProperty는 있지만 SelectedItemProperty는 포함하지 않는 dependencyPropertyFields를 통해 반복됩니다.
private void ValidateBindings(DependencyObject element)
{
Type elementType = element.GetType();
FieldInfo[] dependencyPropertyFields = elementType.GetFields(
BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly);
// Iterate over all dependency properties
foreach (FieldInfo dependencyPropertyField in dependencyPropertyFields)
{
DependencyProperty dependencyProperty =
dependencyPropertyField.GetValue(element) as DependencyProperty;
if (dependencyProperty != null)
{
Binding binding = BindingOperations.GetBinding(element, dependencyProperty);
BindingExpression bindingExpression = BindingOperations.GetBindingExpression(element, dependencyProperty);
// Issue 1822 - Extra check added to prevent null reference exceptions
if (binding != null && bindingExpression != null)
{
// Validate the validation rules of the binding
foreach (ValidationRule rule in binding.ValidationRules)
{
ValidationResult result = rule.Validate(element.GetValue(dependencyProperty),
CultureInfo.CurrentCulture);
bindingExpression.UpdateSource();
if (!result.IsValid)
{
ErrorMessages.Add(result.ErrorContent.ToString());
}
IsContentValid &= result.IsValid;
}
}
}
}
}
어쨌든 내가 잘못 가고있는 부분을 알고 있습니까?
도움을 주시면 큰 도움이됩니다.
감사합니다,
앤디
대단히 감사드립니다. –