在VB中過濾一個wpf collectionviewsource? (Filter a wpf collectionviewsource in VB?)


問題描述

在VB中過濾一個wpf collectionviewsource? (Filter a wpf collectionviewsource in VB?)

I want to filter a collectionviewsource using a filter  I've written, but I'm not sure how I can apply the filter to it?

Here is my collection view source:

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

I have implemented a filter as such:

    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

So how can I get the CollectionViewSource filtered by the filter on load? Could you please give all hte code I need (only a few lines I figure) as I'm quite new to coding.

Thanks guys

EDIT: For the record,

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

gives me the error: 

  

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


參考解法

方法 1:

I'm relatively new to WPF Coding awswell. Here is what I suggest you try:

Make a filter function as follows:

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

Call the function from your Window_Loaded event:

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

Let me know if this doesn't work (May need some adjusting :D )

方法 2:

You should just need to attach the event in the XAML:

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

方法 3:

I had the same issue, till I decided to do the following and works good, I donno what the cons are:

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

Hope this was helpful.

(by Johnny WestlakeJosephGarroneKent BoogaartShimmy Weitzhandler)

參考文件

  1. Filter a wpf collectionviewsource in VB? (CC BY-SA 3.0/4.0)

#collections #filtering #wpf #vb.net






相關問題

C ++中集合/容器的接口/超類 (Interface/Superclass for Collections/Containers in c++)

如何將對象列表轉換為兩級字典? (How to convert a list of objects to two level dictionary?)

如何在java中限制哈希集的默認排序 (how to restrict default ordering of Hash Set in java)

將兩個單獨的 Set 轉換為二維數組 (Convert two separate Sets to a 2D array)

事件調度線程從列表異常 SWING 中移除 (Event dispatching thread remove from List exception SWING)

將集合項複製到 .NET 中的另一個集合 (Copy collection items to another collection in .NET)

java - 如何在java中使用某些特定索引將值存儲到arraylist中 (how to store value into arraylist with some specific index in java)

將對象添加到具有主幹的第一個位置的集合中? (Adding object to a Collection in the first position with backbone?)

在VB中過濾一個wpf collectionviewsource? (Filter a wpf collectionviewsource in VB?)

將流式數據讀入排序列表 (Reading streamed data into a sorted list)

有沒有一種自動處理數組和強類型集合之間轉換的好方法? (Is there a good way to handle conversions between arrays and strongly typed collections automatically?)

將數據行轉換為 1 個對象 (Convert data rows to 1 object)







留言討論