Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am dealing with an application that use CollectionViewSource to display some data on a DatGrid as below:


<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:CaseDataDataSet x:Key="CaseDataDataSet"/>
        <CollectionViewSource x:Key="CasePersonalDataViewSource" Source="{Binding CasePersonalData, Source={StaticResource CaseDataDataSet}}"/>
    </Window.Resources>
    <Grid DataContext="{StaticResource CasePersonalDataViewSource}" >
        <DataGrid x:Name="CasePersonalDataDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding}" Margin="51,46,66,73" RowDetailsVisibilityMode="VisibleWhenSelected">
            <DataGrid.Columns>
                <DataGridTextColumn x:Name="IDColumn" Binding="{Binding ID}" Header="ID" Width="SizeToHeader"/>
                <DataGridCheckBoxColumn x:Name="ActiveColumn" Binding="{Binding Active}" Header="Active" Width="SizeToHeader"/>
                <DataGridTemplateColumn x:Name="CreateDateTimeColumn" Header="Create Date Time" Width="SizeToHeader">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <DatePicker SelectedDate="{Binding CreateDateTime, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn x:Name="UserColumn" Binding="{Binding User}" Header="User" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="CaseNameColumn" Binding="{Binding CaseName}" Header="Case Name" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="CaseFamiliyColumn" Binding="{Binding CaseFamiliy}" Header="Case Familiy" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="IDCardNumberColumn" Binding="{Binding IDCardNumber}" Header="IDCard Number" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="SexColumn" Binding="{Binding Sex}" Header="Sex" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="AgeColumn" Binding="{Binding Age}" Header="Age" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="NationalNumberColumn" Binding="{Binding NationalNumber}" Header="National Number" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="StateColumn" Binding="{Binding State}" Header="State" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="InsuranceOrgColumn" Binding="{Binding InsuranceOrg}" Header="Insurance Org" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="InsuranceNumberColumn" Binding="{Binding InsuranceNumber}" Header="Insurance Number" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="WeightColumn" Binding="{Binding Weight}" Header="Weight" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="HeightColumn" Binding="{Binding Height}" Header="Height" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="CityColumn" Binding="{Binding City}" Header="City" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="ProvinceColumn" Binding="{Binding Province}" Header="Province" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="CountryColumn" Binding="{Binding Country}" Header="Country" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="AddressColumn" Binding="{Binding Address}" Header="Address" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="PhoneNumberColumn" Binding="{Binding PhoneNumber}" Header="Phone Number" Width="SizeToHeader"/>
                <DataGridTemplateColumn x:Name="DateofBirthColumn" Header="Dateof Birth" Width="SizeToHeader">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <DatePicker SelectedDate="{Binding DateofBirth, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>


and this is the behind code

VB
Class MainWindow
    Dim CasePersonalDataViewSource As System.Windows.Data.CollectionViewSource
    Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs) Handles MyBase.Loaded

        Dim CaseDataDataSet As WpfApplication1.CaseDataDataSet = CType(Me.FindResource("CaseDataDataSet"), WpfApplication1.CaseDataDataSet)
        'Load data into the table CasePersonalData. You can modify this code as needed.
        Dim CaseDataDataSetCasePersonalDataTableAdapter As WpfApplication1.CaseDataDataSetTableAdapters.CasePersonalDataTableAdapter = New WpfApplication1.CaseDataDataSetTableAdapters.CasePersonalDataTableAdapter()
        CaseDataDataSetCasePersonalDataTableAdapter.Fill(CaseDataDataSet.CasePersonalData)
        'Load data into the table CasePersonalData. You can modify this code as needed.
        Dim CasePersonalDataViewSource As System.Windows.Data.CollectionViewSource = CType(Me.FindResource("CasePersonalDataViewSource"), System.Windows.Data.CollectionViewSource)
        CasePersonalDataViewSource.View.MoveCurrentToFirst
    End Sub



    Private Sub CollectionViewSource_Filter_1(sender As Object, e As FilterEventArgs)
        Dim CP As CaseDataDataSet.CasePersonalDataRow = CType(e.Item, CaseDataDataSet.CasePersonalDataRow)
        If Not (CP Is Nothing) Then
            'Filter out products with price 25 or above
            If CP.Age > 25 Then
                e.Accepted = True
            Else
                e.Accepted = False
            End If
        End If
    End Sub
End Class


But this code does not work !!!
Any one can help me please ?

What I have tried:

Please help about WPF CollectionViewSource Filter
Posted
Updated 23-Jan-17 7:53am
Comments
[no name] 22-Jan-17 8:17am    
We can't run your code, you will have to use the debugger to figure out what "does not work" means. I would start looking at
'Filter out products with price 25 or above
If CP.Age > 25 Then
first as Age is probably not the price you are looking for.

1 solution

The obvious problem is that you haven't wired up the CollectionViewSource_Filter_1 method to the CollectionViewSource:
XML
<CollectionViewSource 
    x:Key="CasePersonalDataViewSource" 
    Source="{Binding CasePersonalData, Source={StaticResource CaseDataDataSet}}"
    Filter="CollectionViewSource_Filter_1"
/>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900