2013-04-11 2 views
1

동일한 상위 유형에서 모두 상속되는 3 가지 유형의 객체로로드중인 ListBox가 있습니다. CollectionBoxSource를 사용하여 유형에 따라 ListBox의 객체를 필터링하고 싶지만 필터 작성에 문제가 있습니다. 에서Silverlight 4 CollectionViewSource ListBox 데이터 유형 기반 필터

<UserControl.Resources> 
    <CollectionViewSource x:Key="eventsViewSource" 
          Source="{Binding lifeCycleEvents}" > 
     <CollectionViewSource.SortDescriptions> 
      <compMod:SortDescription PropertyName="Date" Direction="Ascending"/> 
     </CollectionViewSource.SortDescriptions> 
    </CollectionViewSource> 
</UserControl.Resources> 


<ListBox Name="lstEventHistory" ItemsSource="{Binding Source={StaticResource eventsViewSource}}"> 
... 
</ListBox> 

코드 숨김 내가 객체의 관찰 컬렉션 비동기 메서드 호출에서 반환 된로드 해요 : : 이제

private ObservableCollection<LifeCycleEvent> lifeCycleEvents; 

... 
       lifeCycleEvents= e.Result; 
       CollectionViewSource eventsViewSource = this.Resources["eventsViewSource"] as CollectionViewSource; 
       eventsViewSource.Source = lifeCycleEvents; 

내가 좋아하는 것 다음과 같이

내 XAML입니다 사용자가 UI에서 클릭 할 체크 박스를 기반으로 특정 유형의 객체 만 표시하는 필터를 작성합니다.

eventsViewSource.Filter = new Predicate<object>(rmaFilter); 

public bool rmaFilter(object item) 
    { 
     if(item.GetType() == typeof(RmaEvent)) 
      return true; 
     else 
      return false; 
    } 

이 다음과 같은 오류를 제공합니다 : "이벤트 System.Windows.Data.CollectionViewSrouce.Filter는 +의 왼쪽에 나타날 수

다음 하나 개의 필터를 작성에 실패한 시도 = 또는 - = ".

필자가 원하는대로 자습서를 찾을 수 없었습니다. Silverlight에서는 존재하지 않는 기본보기를 사용합니다. 내가 본 자습서 중 하나를 찾을 수 있습니다 here

어떤 팁 감사합니다!

답변

1

를 사용하여, 그것을 필터 대리자를 할당 할 수 있습니다. CollectionView의 각 항목에 대해 각 이벤트 처리기가 호출되어 표시되는지 여부를 결정합니다.

또한 Filter 속성에서 이벤트 처리기를 추가하거나 제거하면 이벤트 처리기가 호출되고 컬렉션이 업데이트됩니다.

그래서 필터 이벤트 핸들러를 배선에 대한 올바른 코드는 다음과 같습니다

eventsViewSource.Filter += new FilterEventHandler(rmaFilter); 

그리고 제거 :

eventsViewSource.Filter -= new FilterEventHandler(rmaFilter); 

그리고 새로운 핸들러 :

public void rmaFilter(object sender, FilterEventArgs args) 
{ 
    args.Accepted = args.Item is RmaType; 
} 
0

MSDN 설명서에 따르면 Filter는 CollectionViewSource 형식의 이벤트입니다. 그래서? 올바른 구문은 다음과 같습니다 튜토리얼에서

eventsViewSource.Filter += new Predicate<object>(rmaFilter); 

, 당신은 위의 링크를했습니다, 저자는 CollectionView 개체를 반환하는 CollectionViewSource 유형의 static method 사용. 이 CollectionView 객체는 (! 아직 이벤트) Filter 속성을 가지고 있으며, 당신은 CollectionViewCollectionViewSource의 필터 속성은 이벤트 및 유형 FilterEventHandler하지 Predicate의 컬렉션을 포함 = 구문