2014-09-19 2 views
0

내용이 변경 될 때 ObservableCollection에 ValidationRule을 적용해야합니다. 규칙은 간단하게 체크 경우 collection.Count> 0ObservableCollection의 validationRule

뷰 모델 :

private ObservableCollection<string> _items; 
public ObservableCollection<string> Items 
{ 
    get { return _items; } 
    set { _items = value; OnPropertyChanged("Items"); } 
} 

가 일반보기 예 : 나는 validationRule를 얻이 수없는 것

<ListBox> 
    <ListBox.ItemsSource> 
     <Binding Path="Items" ValidatesOnDataErrors="True"> 
     <Binding.ValidationRules> 
      <a:ValidationRule /> 
     </Binding.ValidationRules> 
     </Binding> 
    </ListBox.ItemsSource> 
    </ListBox> 

해고 할 때 내용이 변경됩니다. 나는 심지어 CollectionChanged를 듣고 BindingExpression과 ValidationRule에 직접 호출을 시도했지만 아직 결과를 얻지 못했다. 이벤트가 발생했지만 바인딩/규칙의 호출은 유효성 검사 시퀀스를 실행하지 않습니다.

//runs the validation, but it does not update the HasError property (appears to just run validation outside of the binding's context 
collection.CollectionChanged += (sender, args) => myValidationRule.Validate(collection, CultureInfo.CurrentCulture); 

//doesn't execute the validation rule.. works for regular bindings - just not ObservableCollection bindings 
collection.CollectionChanged += (sender, args) => myBindingExpression.UpdateSource(); 

답변

0

실제로 소스 업데이트가 작동합니다. 나는 최소한의 테스트를 작성했고 성공했다. 문제는 속성을 호스팅하는 사용자 지정 컨트롤과 관련이 있습니다. 죄송합니다. 조사를 해본 사람들에게는 죄송합니다.

작업 솔루션 :

myBindingExpression = myListBox.GetBindingExpression(ListBox.ItemsSourceProperty) 
collection.CollectionChanged += (sender, args) => myBindingExpression.UpdateSource(); 
관련 문제