2009-04-13 14 views
2

私が書いたフィルタを使用してcollectionviewsourceをフィルタリングしたいのですが、どのようにフィルタを適用することができないのですか?VBでwpf collectionviewsourceをフィルタリングしますか?

<Grid.Resources> 
     <CollectionViewSource x:Key="myCollectionView" 
      Source="{Binding Path=Query4, Source={x:Static Application.Current}}"> 
      <CollectionViewSource.SortDescriptions> 
       <scm:SortDescription PropertyName="ContactID" 
            Direction="Descending"/> 
      </CollectionViewSource.SortDescriptions> 
     </CollectionViewSource> 
    </Grid.Resources> 

私のようなフィルタを実装している:ここで

は私のコレクションビューのソースである

Private Sub WorkerFilter(ByVal sender As Object, ByVal e As FilterEventArgs) 

    Dim value As Object = CType(e.Item, System.Data.DataRow)("StaffSection") 

    If (Not value Is Nothing) And (Not value Is DBNull.Value) Then 
     If (value = "Builder") Or (value = "Office Staff") Then 
      e.Accepted = True 

     Else 

      e.Accepted = False 
     End If 
    End If 
End Sub 

だから私は、負荷のフィルターでろ過しCollectionViewSourceを取得できますか?あなたがコーディングするのが全く新しいので、私が必要とするすべてのhteコードを与えてください。

はみんなありがとう

EDIT:記録のために、

<CollectionViewSource x:Key="myCollectionView" Filter="WorkerFilter" ... /> 

は私にエラーを与える:

Failed object initialization (ISupportInitialize.EndInit). 'System.Windows.Data.BindingListCollectionView' view does not support filtering. Error at object 'myCollectionView'

答えて

0

あなただけXAMLでイベントを添付する必要がありますする必要があります

<CollectionViewSource x:Key="myCollectionView" Filter="WorkerFilter" ...> 
+1

ありがとうございますが、私が試してみると、エラーが発生しました: オブジェクトの初期化に失敗しました(ISupportInitialize.EndInit)。 'System.Windows.Data.BindingListCollectionView'ビューはフィルタリングをサポートしていません。オブジェクト 'myCollectionView'のエラー –

+0

CollectionViewのソースは、BindingListに基づいているため、フィルタリングをサポートしていません。なぜBindingListがフィルタリングをサポートしていないのか分かりませんが、普通の古いリストはそうしています。 BindingList ではなく、リストにソースを変更してみてください。 –

+0

Uuuhhh ....申し訳ありませんが、私はそれについてどうやって行くのか分かりません。私は "Source =" {Binding Path = Query4、Source = {x:Static Application.Current}} "が私のバインディングリストであると仮定します。これは、Query4という名前のデータテーブルを参照します。どのように私はこれを普通のリストにすることができますか? ありがとう –

0

私は次のことを行うことを決めたといい作品までと同じ問題が、私はdonno短所は何をしている:これは役立ちました

<Window 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:data="clr-namespace:System.Windows.Data;assembly=PresentationFramework"> 

    <CollectionViewSource 
    x:Key="FilteredBindingListCollection" 
    CollectionViewType="{x:Type data:ListCollectionView}" /> 

</Window> 

希望。

1

私はWPsw Coding awswellには比較的新しいです。ここで私はあなたがしようと提案するものです。

次のようにフィルタ機能を作成します。

Public Function FilterList(item As Object) As Boolean 
     Dim value as Object = item 
     If (Not value Is Nothing) And (Not value Is DBNull.Value) Then 
     If (value = "Builder") Or (value = "Office Staff") Then 
      Return True 

     Else 

      Return False 
     End If 
    End If 
End Function 

は、あなたのWindow_Loadedイベントから関数を呼び出します。

Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles MainWindow.Loaded 
    MyCollectionView = CollectionViewSource.GetDefaultView(Query4) 'May not be needed, IDK 
    MyCollectionView.Filter = New Predicate(Of Object)(AddressOf FilterList) 
End Sub 

が、これは動作しない場合は、私に教えてください(D調整が必要な場合があります)

関連する問題