本文介绍了如何过滤WPF collectionviewsource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个使用CollectionViewSource在DatGrid上显示一些数据的应用程序,如下所示:





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

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 Filter



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

What I have tried:

Please help about WPF CollectionViewSource Filter

推荐答案

<CollectionViewSource 

    x:Key="CasePersonalDataViewSource" 

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

    Filter="CollectionViewSource_Filter_1"

/>


这篇关于如何过滤WPF collectionviewsource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 01:12