2009-07-01 8 views
2

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; 
        } 
       } 
      } 
     } 
    } 

어쨌든 내가 잘못 가고있는 부분을 알고 있습니까?

도움을 주시면 큰 도움이됩니다.

감사합니다,

앤디

답변

3

당신은 SelectedItemProperty을 찾는되지는 콤보 doesn't have SelectedItemProperty 필드 있기 때문에, 대신이 상속되는 것이 기본 클래스 Selector의에서. 물론 Selector와 ComboBox에는 둘 중 하나에 바인딩 할 수있는 모든 속성이 없기 때문에 대부분의 상속 된 속성을 찾으려면 UIElement으로 다시 이동해야합니다.

상속 계층을 트래버스하기 위해 무언가를 삽입하면 모든 필드를 가져올 수 있으며 유효성 검사 규칙이 실행됩니다.

List<FieldInfo> dependencyPropertyFields = elementType.GetFields(
    BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly).ToList(); 

// Iterate through the fields defined on any base types as well 
Type baseType = elementType.BaseType; 
while (baseType != null) 
{ 
    dependencyPropertyFields.AddRange(
     baseType.GetFields(
      BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly)); 

    baseType = baseType.BaseType; 
} 
+0

대단히 감사드립니다. –

관련 문제