mehdishahgholi Ответов: 1

Как отфильтровать WPF collectionviewsource


Я имею дело с приложением, которое использует CollectionViewSource для отображения некоторых данных на DatGrid, как показано ниже:


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


и это скрытый код

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


Но этот код не работает !!!
Кто-нибудь может мне помочь, пожалуйста ?

Что я уже пробовал:

Помогите, пожалуйста, насчет фильтра в WPF классу collectionviewsource

[no name]

Мы не можем запустить ваш код, вам придется использовать отладчик, чтобы выяснить, что означает "не работает". Я бы начал смотреть на
"Отфильтруйте продукты с ценой 25 или выше
Если КП.Возраст > 25 тогда
во-первых, возраст-это, вероятно, не та цена, которую вы ищете.

1 Ответов

Рейтинг:
0

Richard Deeming

Очевидная проблема заключается в том, что вы не подключили CollectionViewSource_Filter_1 метод к CollectionViewSource:

<CollectionViewSource 

    x:Key="CasePersonalDataViewSource" 

    Source="{Binding CasePersonalData, Source={StaticResource CaseDataDataSet}}"

    Filter="CollectionViewSource_Filter_1"

/>