亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

WPF實(shí)現(xiàn)帶篩選功能的DataGrid

 更新時(shí)間:2023年03月01日 08:39:40   作者:小六公子  
在默認(rèn)情況下,WPF提供的DataGrid僅擁有數(shù)據(jù)展示等簡(jiǎn)單功能,如果要實(shí)現(xiàn)像Excel一樣復(fù)雜的篩選過(guò)濾功能,則相對(duì)比較麻煩。本文以一個(gè)簡(jiǎn)單的小例子,簡(jiǎn)述如何通過(guò)WPF實(shí)現(xiàn)DataGrid的篩選功能,僅供學(xué)習(xí)分享使用,如有不足之處,還請(qǐng)指正

涉及知識(shí)點(diǎn)

在本示例中,從數(shù)據(jù)綁定,到數(shù)據(jù)展示,涉及知識(shí)點(diǎn)如下所示:

  • DataGrid,要WPF提供的進(jìn)行二維數(shù)據(jù)展示在列表控件,默認(rèn)功能非常簡(jiǎn)單,但是可以通過(guò)數(shù)據(jù)模板或者控件模板進(jìn)行擴(kuò)展和美化,可伸縮性很強(qiáng)。
  • MVVM,是Model-View-ViewModel的簡(jiǎn)寫(xiě),主要進(jìn)行數(shù)據(jù)和UI進(jìn)行前后端分離,在本示例中,主要用到的MVVM第三方庫(kù)為CommunityToolkit.Mvvm,大大簡(jiǎn)化原生MVVM的實(shí)現(xiàn)方式。
  • 集合視圖, 要對(duì) DataGrid 中的數(shù)據(jù)進(jìn)行分組、排序和篩選,可以將其綁定到支持這些函數(shù)的 CollectionView。 然后,可以在不影響基礎(chǔ)源數(shù)據(jù)的情況下處理 CollectionView 中的數(shù)據(jù)。 集合視圖中的更改反映在 DataGrid 用戶界面 (UI) 中。
  • Popup控件,直接繼承FrameworkElement,提供了一種在單獨(dú)的窗口中顯示內(nèi)容的方法,該窗口相對(duì)于指定的元素或屏幕坐標(biāo),浮動(dòng)在當(dāng)前Popup應(yīng)用程序窗口上,可用于懸浮窗口。

示例截圖

本示例主要模仿Excel的篩選功能進(jìn)行實(shí)現(xiàn),右鍵標(biāo)題欄打開(kāi)浮動(dòng)窗口,懸浮于標(biāo)題欄下方,既可以通過(guò)文本框進(jìn)行篩選,也可以通過(guò)篩選按鈕彈出右鍵菜單,選擇具體篩選方式,截圖如下所示:

選擇篩選方式,彈出窗口,如下所示:

輸入篩選條件,點(diǎn)擊確定,或者取消篩選。如篩選學(xué)號(hào)里面包含2的,效果如下所示:

 注意:以上篩選都是客戶端篩選,不會(huì)修改數(shù)據(jù)源,也不會(huì)重連數(shù)據(jù)庫(kù)。

核心源碼

在本示例中,核心源碼主要包含以下幾個(gè)部分:

前端視圖【MainWindow.xaml】源碼

主要實(shí)現(xiàn)了按學(xué)號(hào),姓名,年齡三列進(jìn)行篩選,既可以單列篩選,又可以組合篩選。且三列的篩選實(shí)現(xiàn)方式一致,僅是綁定列有差異。

<Window x:Class="DemoDataGrid.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:DemoDataGrid"
        mc:Ignorable="d"
        Title="DataGrid篩選示例" Height="650" Width="800">
    <Window.Resources>
        <ResourceDictionary>
            <CollectionViewSource x:Key="ItemsSource" Source="{Binding Path=Students}"></CollectionViewSource>
            <CollectionViewSource x:Key="Names" Source="{Binding Path=Names}"></CollectionViewSource>
            <CollectionViewSource x:Key="Nos" Source="{Binding Path=Nos}"></CollectionViewSource>
            <CollectionViewSource x:Key="Ages" Source="{Binding Path=Ages}"></CollectionViewSource>
            <Style x:Key="ListBoxStyle" TargetType="{x:Type ListBox}">
                <Setter Property="ScrollViewer.CanContentScroll" Value="True"></Setter>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListBox}">
                            <ScrollViewer x:Name="ScrollViewer" CanContentScroll="True">
                                <ItemsPresenter></ItemsPresenter>
                            </ScrollViewer>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
            <Geometry x:Key="Icon_Filter">
                M608 864C588.8 864 576 851.2 576 832L576 448c0-6.4 6.4-19.2 12.8-25.6L787.2 256c6.4-6.4 6.4-19.2 0-19.2 0-6.4-6.4-12.8-19.2-12.8L256 224c-12.8 0-19.2 6.4-19.2 12.8 0 6.4-6.4 12.8 6.4 19.2l198.4 166.4C441.6 428.8 448 441.6 448 448l0 256c0 19.2-12.8 32-32 32S384 723.2 384 704L384 460.8 198.4 307.2c-25.6-25.6-32-64-19.2-96C185.6 179.2 217.6 160 256 160L768 160c32 0 64 19.2 76.8 51.2 12.8 32 6.4 70.4-19.2 89.6l-192 160L633.6 832C640 851.2 627.2 864 608 864z
            </Geometry>
            <ContextMenu x:Key="queryConditionMenu" MouseLeave="ContextMenu_MouseLeave" MenuItem.Click="ContextMenu_Click">
                <MenuItem FontSize="12" Header="等于" Tag="Equal"></MenuItem>
                <MenuItem FontSize="12" Header="不等于"  Tag="NotEqual"></MenuItem>
                <MenuItem FontSize="12" Header="開(kāi)頭"  Tag="Begin"></MenuItem>
                <MenuItem FontSize="12" Header="結(jié)尾"  Tag="End"></MenuItem>
                <MenuItem FontSize="12" Header="包含"  Tag="In"></MenuItem>
                <MenuItem FontSize="12" Header="不包含"  Tag="NotIn"></MenuItem>
            </ContextMenu>
        </ResourceDictionary>

    </Window.Resources>
    <Grid Margin="10">
        <Grid.RowDefinitions>
            <RowDefinition Height="20"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <DataGrid Grid.Row="1" x:Name="dgStudents" ItemsSource="{Binding Source={StaticResource ItemsSource} }" AutoGenerateColumns="False"
                              CanUserReorderColumns="True" CanUserDeleteRows="False" CanUserAddRows="False" HeadersVisibility="Column"
                              CanUserSortColumns="True"
                              VirtualizingPanel.VirtualizationMode="Recycling"
                              EnableColumnVirtualization="True" VirtualizingPanel.IsVirtualizingWhenGrouping="True" GridLinesVisibility="All" RowHeight="25"
                              SelectionUnit="FullRow" SelectionMode="Single" IsReadOnly="True" FontSize="12"
                              SelectedIndex="{Binding SelectTaskItemIndex}" SelectedItem="{Binding SelectTaskItem}"
                              CanUserResizeColumns="True">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Id}" Header="Id" Width="*">

                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding No}" Width="*">
                    <DataGridTextColumn.Header>
                        <TextBlock Text="學(xué)號(hào)" FontWeight="Regular" MouseRightButtonDown="TextBlock_MouseRightButtonDown" Tag="No"></TextBlock>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding Name}" Width="*">
                    <DataGridTextColumn.Header>
                        <TextBlock Text="姓名" FontWeight="Regular" MouseRightButtonDown="TextBlock_MouseRightButtonDown" Tag="Name"></TextBlock>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding Age}" Width="*">
                    <DataGridTextColumn.Header>
                        <TextBlock Text="年齡" FontWeight="Regular" MouseRightButtonDown="TextBlock_MouseRightButtonDown" Tag="Age"></TextBlock>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
        <Popup x:Name="popupNo" Width="135" MaxHeight="500" Height="Auto" PopupAnimation="Slide" AllowsTransparency="False" MouseLeave="popup_MouseLeave">
            <Border BorderBrush="LightBlue" BorderThickness="1" Padding="5" Background="AliceBlue">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition></ColumnDefinition>
                        <ColumnDefinition></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition></RowDefinition>
                        <RowDefinition></RowDefinition>
                        <RowDefinition></RowDefinition>
                    </Grid.RowDefinitions>
                    <StackPanel Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Orientation="Horizontal">
                        <TextBox Height="25" x:Name="txtNo" MinWidth="60" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Tag="No" TextChanged="TextBox_TextChanged"></TextBox>
                        <Button x:Name="btnNoFilter"  Tag="No" ClickMode="Press" Click="ButtonFilter_Click" ContextMenu="{StaticResource queryConditionMenu}">
                            <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                                <Path Data="{StaticResource Icon_Filter}" Stroke="Gray" StrokeThickness="1"
                       Height="12" Width="12" Stretch="Fill"></Path>
                                <TextBlock Margin="2,0" Text="篩選" FontSize="12"></TextBlock>
                            </StackPanel>
                        </Button>
                    </StackPanel>
                    <ListBox x:Name="lbNos" ItemsSource="{Binding Source={StaticResource Nos}}" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" VirtualizingPanel.VirtualizationMode="Recycling" VirtualizingPanel.IsVirtualizing="True" Style="{StaticResource ListBoxStyle}">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <CheckBox Content="{Binding FilterText}" IsChecked="{Binding IsChecked}"></CheckBox>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

                    <Button Tag="No" Content="取消" Width="60" HorizontalAlignment="Left" Grid.Row="2" Grid.Column="0" Click="btnCancel_Click"></Button>
                    <Button Content="確定" Width="60" HorizontalAlignment="Right" Grid.Row="2" Grid.Column="1" Click="btnOk_Click"></Button>
                </Grid>
            </Border>
        </Popup>
        <Popup x:Name="popupName" Width="135" MaxHeight="500" Height="Auto" PopupAnimation="Slide" AllowsTransparency="False" MouseLeave="popup_MouseLeave">
            <Border BorderBrush="LightBlue" BorderThickness="1" Padding="5" Background="AliceBlue">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition></ColumnDefinition>
                        <ColumnDefinition></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition></RowDefinition>
                        <RowDefinition></RowDefinition>
                        <RowDefinition></RowDefinition>
                    </Grid.RowDefinitions>
                    <StackPanel Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Orientation="Horizontal">
                        <TextBox Height="25" x:Name="txtName" MinWidth="60" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Tag="Name" TextChanged="TextBox_TextChanged"></TextBox>
                        <Button x:Name="btnNameFilter"  Tag="Name" Click="ButtonFilter_Click" ContextMenu="{StaticResource queryConditionMenu}">
                            <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                                <Path Data="{StaticResource Icon_Filter}" Stroke="Gray" StrokeThickness="1"
                       Height="12" Width="12" Stretch="Fill"></Path>
                                <TextBlock Margin="2,0" Text="篩選" FontSize="12"></TextBlock>
                            </StackPanel>
                        </Button>
                    </StackPanel>
                    <ListBox x:Name="lbNames" ItemsSource="{Binding Source={StaticResource Names}}" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" VirtualizingPanel.VirtualizationMode="Recycling" VirtualizingPanel.IsVirtualizing="True" Style="{StaticResource ListBoxStyle}">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <CheckBox Content="{Binding FilterText}" IsChecked="{Binding IsChecked}"></CheckBox>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

                    <Button Tag="No" Content="取消" Width="60" HorizontalAlignment="Left" Grid.Row="2" Grid.Column="0" Click="btnCancel_Click"></Button>
                    <Button Content="確定" Width="60" HorizontalAlignment="Right" Grid.Row="2" Grid.Column="1" Click="btnOk_Click"></Button>
                </Grid>
            </Border>
        </Popup>
        <Popup x:Name="popupAge" Width="135" MaxHeight="500" Height="Auto" PopupAnimation="Slide" AllowsTransparency="False" MouseLeave="popup_MouseLeave">
            <Border BorderBrush="LightBlue" BorderThickness="1" Padding="5" Background="AliceBlue">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition></ColumnDefinition>
                        <ColumnDefinition></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition></RowDefinition>
                        <RowDefinition></RowDefinition>
                        <RowDefinition></RowDefinition>
                    </Grid.RowDefinitions>
                    <StackPanel Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Orientation="Horizontal">
                        <TextBox Height="25" x:Name="txtAge" MinWidth="60" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Tag="Age" TextChanged="TextBox_TextChanged"></TextBox>
                        <Button x:Name="btnAgeFilter"  Tag="Age" Click="ButtonFilter_Click" ContextMenu="{StaticResource queryConditionMenu}">
                            <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                                <Path Data="{StaticResource Icon_Filter}" Stroke="Gray" StrokeThickness="1"
                       Height="12" Width="12" Stretch="Fill"></Path>
                                <TextBlock Margin="2,0" Text="篩選" FontSize="12"></TextBlock>
                            </StackPanel>
                        </Button>
                    </StackPanel>
                    <ListBox x:Name="lbAges" ItemsSource="{Binding Source={StaticResource Ages}}" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" VirtualizingPanel.VirtualizationMode="Recycling" VirtualizingPanel.IsVirtualizing="True" Style="{StaticResource ListBoxStyle}">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <CheckBox Content="{Binding FilterText}" IsChecked="{Binding IsChecked}"></CheckBox>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

                    <Button Tag="No" Content="取消" Width="60" HorizontalAlignment="Left" Grid.Row="2" Grid.Column="0" Click="btnCancel_Click"></Button>
                    <Button Content="確定" Width="60" HorizontalAlignment="Right" Grid.Row="2" Grid.Column="1" Click="btnOk_Click"></Button>
                </Grid>
            </Border>
        </Popup>

        <Popup x:Name="popupNoMenu" Width="300" MaxHeight="500" Height="200" PopupAnimation="Slide" AllowsTransparency="False" Tag="">
            <Border BorderThickness="1" BorderBrush="Beige" Padding="15" Background="AliceBlue">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition></ColumnDefinition>
                        <ColumnDefinition></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"></RowDefinition>
                        <RowDefinition Height="Auto"></RowDefinition>
                        <RowDefinition Height="Auto"></RowDefinition>
                        <RowDefinition></RowDefinition>
                    </Grid.RowDefinitions>
                    <StackPanel Orientation="Horizontal" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2">
                        <TextBlock Text="學(xué)號(hào)" VerticalAlignment="Center"></TextBlock>
                        <ComboBox x:Name="combNoMenu1" Height="28" Width="100" Margin="4" VerticalContentAlignment="Center">
                            <ComboBoxItem Content="等于" ></ComboBoxItem>
                            <ComboBoxItem Content="不等于"></ComboBoxItem>
                            <ComboBoxItem Content="開(kāi)頭"></ComboBoxItem>
                            <ComboBoxItem Content="結(jié)尾"></ComboBoxItem>
                            <ComboBoxItem Content="包含"></ComboBoxItem>
                            <ComboBoxItem Content="不包含"></ComboBoxItem>
                        </ComboBox>
                        <TextBox x:Name="txtNoMenu1" Height="28" Width="100" VerticalContentAlignment="Center"></TextBox>
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" Grid.Row="1" Grid.Column="0"  Grid.ColumnSpan="2">
                        <RadioButton x:Name="rbNoAnd" Content="與" IsChecked="True" Margin="4" Background="AliceBlue" VerticalAlignment="Center" VerticalContentAlignment="Center"></RadioButton>
                        <RadioButton x:Name="rbNoOr" Content="或" Background="AliceBlue" VerticalAlignment="Center" VerticalContentAlignment="Center"></RadioButton>
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" Grid.Row="2" Grid.Column="0"  Grid.ColumnSpan="2">
                        <TextBlock Text="學(xué)號(hào)" VerticalAlignment="Center"></TextBlock>
                        <ComboBox x:Name="combNoMenu2" Height="28" Margin="4" Width="100" VerticalContentAlignment="Center">
                            <ComboBoxItem Content="等于" ></ComboBoxItem>
                            <ComboBoxItem Content="不等于"></ComboBoxItem>
                            <ComboBoxItem Content="開(kāi)頭"></ComboBoxItem>
                            <ComboBoxItem Content="結(jié)尾"></ComboBoxItem>
                            <ComboBoxItem Content="包含"></ComboBoxItem>
                            <ComboBoxItem Content="不包含"></ComboBoxItem>
                        </ComboBox>
                        <TextBox x:Name="txtNoMenu2" Height="28" Width="100" VerticalContentAlignment="Center"></TextBox>
                    </StackPanel>
                    <Button Tag="No" Content="取消" Width="100" Height="28" HorizontalAlignment="Left" Grid.Row="3" Grid.Column="0" Click="btnCancelFilter_Click"></Button>
                    <Button Tag="No" Content="確定" Width="100" Height="28" HorizontalAlignment="Right" Grid.Row="3" Grid.Column="1" Click="btnOkFilter_Click"></Button>
                </Grid>
            </Border>
        </Popup>
        <Popup x:Name="popupNameMenu" Width="300" MaxHeight="500" Height="200" PopupAnimation="Slide" AllowsTransparency="False" Tag="">
            <Border BorderThickness="1" BorderBrush="Beige" Padding="15" Background="AliceBlue">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"></RowDefinition>
                        <RowDefinition Height="Auto"></RowDefinition>
                        <RowDefinition Height="Auto"></RowDefinition>
                        <RowDefinition Height="Auto"></RowDefinition>
                    </Grid.RowDefinitions>
                    <StackPanel Orientation="Horizontal" Grid.Row="0" Grid.Column="0">
                        <TextBlock Text="姓名" VerticalAlignment="Center"></TextBlock>
                        <ComboBox x:Name="combNameMenu1" Height="28" Width="100" Margin="4" VerticalContentAlignment="Center">
                            <ComboBoxItem Content="等于" ></ComboBoxItem>
                            <ComboBoxItem Content="不等于"></ComboBoxItem>
                            <ComboBoxItem Content="開(kāi)頭"></ComboBoxItem>
                            <ComboBoxItem Content="結(jié)尾"></ComboBoxItem>
                            <ComboBoxItem Content="包含"></ComboBoxItem>
                            <ComboBoxItem Content="不包含"></ComboBoxItem>
                        </ComboBox>
                        <TextBox x:Name="txtNameMenu1" Height="28" Width="100" VerticalContentAlignment="Center"></TextBox>
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" Grid.Row="1" Grid.Column="0">
                        <RadioButton x:Name="rbNameAnd" Content="與" IsChecked="True" Margin="4" Background="AliceBlue" VerticalAlignment="Center" VerticalContentAlignment="Center"></RadioButton>
                        <RadioButton x:Name="rbNameOr" Content="或" Background="AliceBlue" VerticalAlignment="Center" VerticalContentAlignment="Center"></RadioButton>
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" Grid.Row="2" Grid.Column="0">
                        <TextBlock Text="姓名" VerticalAlignment="Center"></TextBlock>
                        <ComboBox x:Name="combNameMenu2" Height="28" Width="100" Margin="4" VerticalContentAlignment="Center">
                            <ComboBoxItem Content="等于" ></ComboBoxItem>
                            <ComboBoxItem Content="不等于"></ComboBoxItem>
                            <ComboBoxItem Content="開(kāi)頭"></ComboBoxItem>
                            <ComboBoxItem Content="結(jié)尾"></ComboBoxItem>
                            <ComboBoxItem Content="包含"></ComboBoxItem>
                            <ComboBoxItem Content="不包含"></ComboBoxItem>
                        </ComboBox>
                        <TextBox x:Name="txtNameMenu2" Height="28" Width="100" VerticalContentAlignment="Center"></TextBox>
                    </StackPanel>
                    <Button Tag="Name" Content="取消" Width="100" Height="28" HorizontalAlignment="Left" Grid.Row="3" Grid.Column="0" Click="btnCancelFilter_Click"></Button>
                    <Button Tag="Name" Content="確定" Width="100" Height="28" HorizontalAlignment="Right" Grid.Row="3" Grid.Column="1" Click="btnOkFilter_Click"></Button>
                </Grid>
            </Border>
        </Popup>
        <Popup x:Name="popupAgeMenu" Width="300" MaxHeight="500" Height="200" PopupAnimation="Slide" AllowsTransparency="False" Tag="">
            <Border BorderThickness="1" BorderBrush="Beige" Padding="15" Background="AliceBlue">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"></RowDefinition>
                        <RowDefinition Height="Auto"></RowDefinition>
                        <RowDefinition Height="Auto"></RowDefinition>
                        <RowDefinition Height="Auto"></RowDefinition>
                    </Grid.RowDefinitions>
                    <StackPanel Orientation="Horizontal" Grid.Row="0" Grid.Column="0">
                        <TextBlock Text="年齡" VerticalAlignment="Center"></TextBlock>
                        <ComboBox x:Name="combAgeMenu1" Height="28" Width="100" Margin="4" VerticalContentAlignment="Center">
                            <ComboBoxItem Content="等于" ></ComboBoxItem>
                            <ComboBoxItem Content="不等于"></ComboBoxItem>
                            <ComboBoxItem Content="開(kāi)頭"></ComboBoxItem>
                            <ComboBoxItem Content="結(jié)尾"></ComboBoxItem>
                            <ComboBoxItem Content="包含"></ComboBoxItem>
                            <ComboBoxItem Content="不包含"></ComboBoxItem>
                        </ComboBox>
                        <TextBox x:Name="txtAgeMenu1" Height="28" Width="100" VerticalContentAlignment="Center"></TextBox>
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" Grid.Row="1" Grid.Column="0">
                        <RadioButton x:Name="rbAgeAnd" Content="與"  IsChecked="True" Margin="4" Background="AliceBlue" VerticalAlignment="Center" VerticalContentAlignment="Center"></RadioButton>
                        <RadioButton x:Name="rbAgeOr" Content="或" Background="AliceBlue" VerticalAlignment="Center" VerticalContentAlignment="Center"></RadioButton>
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" Grid.Row="2" Grid.Column="0">
                        <TextBlock Text="年齡" VerticalAlignment="Center"></TextBlock>
                        <ComboBox x:Name="combAgeMenu2" Height="28" Width="100" Margin="4" VerticalContentAlignment="Center">
                            <ComboBoxItem Content="等于" ></ComboBoxItem>
                            <ComboBoxItem Content="不等于"></ComboBoxItem>
                            <ComboBoxItem Content="開(kāi)頭"></ComboBoxItem>
                            <ComboBoxItem Content="結(jié)尾"></ComboBoxItem>
                            <ComboBoxItem Content="包含"></ComboBoxItem>
                            <ComboBoxItem Content="不包含"></ComboBoxItem>
                        </ComboBox>
                        <TextBox x:Name="txtAgeMenu2" Height="28" Width="100" VerticalContentAlignment="Center"></TextBox>
                    </StackPanel>
                    <Button Tag="Age" Content="取消" Width="100" Height="28" HorizontalAlignment="Left" Grid.Row="3" Grid.Column="0" Click="btnCancelFilter_Click"></Button>
                    <Button Tag="Age" Content="確定" Width="100" Height="28" HorizontalAlignment="Right" Grid.Row="3" Grid.Column="1" Click="btnOkFilter_Click"></Button>
                </Grid>
            </Border>
        </Popup>
    </Grid>
</Window>

業(yè)務(wù)邏輯【MainWindowViewModel】

業(yè)務(wù)邏輯處理主要復(fù)責(zé)數(shù)據(jù)初始化等業(yè)務(wù)相關(guān)內(nèi)容,和UI無(wú)關(guān),如下所示:

using CommunityToolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DemoDataGrid
{
    public class MainWindowViewModel:ObservableObject
    {
        #region 屬性及構(gòu)造函數(shù)

        private List<Student> students;

        public List<Student> Students
        {
            get { return students; }
            set { SetProperty(ref students, value); }
        }

        private List<FilterInfo> names;

        public List<FilterInfo> Names
        {
            get { return names; }
            set { SetProperty(ref names, value); }
        }

        private List<FilterInfo> nos;

        public List<FilterInfo> Nos
        {
            get { return nos; }
            set {SetProperty(ref nos , value); }
        }

        private List<FilterInfo> ages;

        public List<FilterInfo> Ages
        {
            get { return ages; }
            set {SetProperty(ref ages , value); }
        }



        public MainWindowViewModel()
        {
            this.Students= new List<Student>();
            for (int i = 0; i < 20; i++) {
                this.Students.Add(new Student()
                {
                    Id = i,
                    Name = $"張{i}牛",
                    Age = (i % 10) + 10,
                    No = i.ToString().PadLeft(4, '0'),
                });
            }
            this.Nos= new List<FilterInfo>();
            this.Names= new List<FilterInfo>();
            this.Ages= new List<FilterInfo>();
            this.Students.ForEach(s => {
                this.Nos.Add(new FilterInfo() { FilterText=s.No,IsChecked=false });
                this.Names.Add(new FilterInfo() { FilterText = s.Name, IsChecked = false });
                this.Ages.Add(new FilterInfo() { FilterText = s.Age.ToString(), IsChecked = false });
            });
            this.Ages=this.Ages.Distinct().ToList();//去重
        }

        #endregion


    }
}

篩選功能實(shí)現(xiàn)【MainWindow.xaml.cs】

本示例為了簡(jiǎn)化實(shí)現(xiàn),篩選功能處理主要在cs后端實(shí)現(xiàn),如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DemoDataGrid
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private MainWindowViewModel viewModel;

        public MainWindow()
        {
            InitializeComponent();
            viewModel = new MainWindowViewModel();
            this.DataContext = viewModel;
        }


        #region 篩選

        private void TextBlock_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (sender != null && sender is TextBlock)
            {
                var textBlock = sender as TextBlock;
                var tag = textBlock.Tag.ToString();
                var pop = this.FindName($"popup{tag}");
                if (pop != null)
                {
                    var popup = pop as System.Windows.Controls.Primitives.Popup;
                    if (popup != null)
                    {
                        popup.IsOpen = true;
                        popup.PlacementTarget = textBlock;
                        popup.Placement = System.Windows.Controls.Primitives.PlacementMode.RelativePoint;
                        popup.VerticalOffset = 10;
                        popup.HorizontalOffset = 10;
                    }
                }
            }
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox textBox = e.OriginalSource as TextBox;
            var tag = textBox.Tag;//條件
            var text = textBox.Text;
            if (tag != null)
            {
                if (tag.ToString() == "No")
                {
                    Filter(this.lbNos.ItemsSource, this.txtNo.Text);
                }
                if (tag.ToString() == "Name")
                {
                    Filter(this.lbNames.ItemsSource, this.txtName.Text);
                }
                if (tag.ToString() == "Age")
                {
                    Filter(this.lbAges.ItemsSource, this.txtAge.Text);
                }
            }

        }

        private void Filter(object source, string filter)
        {
            var cv = CollectionViewSource.GetDefaultView(source);
            if (cv != null && cv.CanFilter)
            {
                cv.Filter = new Predicate<object>((obj) => {
                    bool flag = true;
                    var t = obj as FilterInfo;
                    if (t != null)
                    {
                        flag = t.FilterText.Contains(filter);
                    }
                    return flag;
                });
            }
        }

        private void popup_MouseLeave(object sender, MouseEventArgs e)
        {
            var popup = e.OriginalSource as System.Windows.Controls.Primitives.Popup;
            var showContext = (this.FindResource("queryConditionMenu") as ContextMenu)?.IsOpen;
            if (popup != null && showContext==false)
            {
                popup.IsOpen = false;
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            var btn = e.OriginalSource as Button;
            if (btn != null)
            {
                var tag = btn.Tag;
                if (tag.ToString() == "No")
                {
                    ClearFilter(this.txtNo, this.viewModel.Nos);
                }
                if (tag.ToString() == "Name")
                {
                    ClearFilter(this.txtName, this.viewModel.Names);

                }
                if (tag.ToString() == "Age")
                {
                    ClearFilter(this.txtAge, this.viewModel.Ages);
                }
                FilterTask();//清除以后,重新刷新
            }
        }

        private void ClearFilter(TextBox textBox, List<FilterInfo> collection)
        {
            textBox.Clear();
            foreach (var f in collection)
            {
                f.IsChecked = false;
            }
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            //
            FilterTask();
        }


        private void FilterTask()
        {
            var cv = CollectionViewSource.GetDefaultView(this.dgStudents.ItemsSource);
            if (cv != null && cv.CanFilter)
            {
                cv.Filter = new Predicate<object>((obj) =>
                {
                    bool flag = true;
                    var t = obj as Student;
                    if (t != null)
                    {
                        var nos = this.viewModel.Nos.Where(r => r.IsChecked == true).ToList();
                        var names = this.viewModel.Names.Where(r => r.IsChecked == true).ToList();
                        var ages = this.viewModel.Ages.Where(r => r.IsChecked == true).ToList();
                        if (nos.Count() > 0)
                        {
                            flag = flag && nos.Select(r => r.FilterText).Contains(t.No);
                        }
                        if (names.Count() > 0)
                        {
                            flag = flag && names.Select(r => r.FilterText).Contains(t.Name);
                        }
                        if (ages.Count() > 0)
                        {
                            flag = flag && ages.Select(r => r.FilterText).Contains(t.Age.ToString());
                        }
                    }
                    return flag;
                });
            }
        }

        #endregion

        private List<string> condition = new List<string>() { "Equal", "NotEqual", "Begin", "End", "In", "NotIn" };

        private void ButtonFilter_Click(object sender, RoutedEventArgs e)
        {
            var btn = e.OriginalSource as Button;
            if (btn != null)
            {
                var tag = btn.Tag;
                var popup = this.FindName($"popup{tag}") as System.Windows.Controls.Primitives.Popup;
                if (popup != null)
                {
                    popup.IsOpen = true;
                }
                if (btn.ContextMenu.IsOpen)
                {
                    btn.ContextMenu.IsOpen = false;
                }
                else
                {
                    btn.ContextMenu.Tag = tag;
                    btn.ContextMenu.Width = 100;
                    btn.ContextMenu.Height = 150;
                    btn.ContextMenu.IsOpen = true;
                    btn.ContextMenu.PlacementTarget = btn;
                    btn.ContextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
                }
            }
        }

        private void ContextMenu_MouseLeave(object sender, MouseEventArgs e)
        {
            var menu = e.OriginalSource as ContextMenu;
            if (menu != null)
            {
                menu.IsOpen = false;
            }
        }

        private void ContextMenu_Click(object sender, RoutedEventArgs e)
        {
            var contextMenu = sender as ContextMenu;
            if (contextMenu == null)
            {
                return;
            }
            var menuItem = e.OriginalSource as MenuItem;
            if (menuItem == null)
            {
                return;
            }
            var tag1 = contextMenu.Tag.ToString();//點(diǎn)擊的哪一個(gè)按鈕
            var tag2 = menuItem.Tag.ToString();//點(diǎn)擊的是哪一個(gè)菜單
            var pop = this.FindName($"popup{tag1}Menu");
            var comb = this.FindName($"comb{tag1}Menu1");
            HideParentPopup(tag1);//隱藏父Popup
            if (comb != null)
            {
                var combMenu = comb as ComboBox;
                combMenu.SelectedIndex = condition.IndexOf(tag2);
            }
            if (pop != null)
            {
                var popup = pop as System.Windows.Controls.Primitives.Popup;
                popup.IsOpen = true;
                popup.PlacementTarget = dgStudents;
                popup.Placement = System.Windows.Controls.Primitives.PlacementMode.Center;
            }
        }

        private void btnCancelFilter_Click(object sender, RoutedEventArgs e)
        {
            if (sender == null)
            {
                return;
            }
            var btn = sender as System.Windows.Controls.Button;
            if (btn != null)
            {
                var tag = btn.Tag.ToString();
                HidePopupMenu(tag);//隱藏Popup控件
                if (tag == "No")
                {
                    ClearMenuFilter(this.txtNoMenu1, this.txtNoMenu2);
                }
                if (tag == "Name")
                {
                    ClearMenuFilter(this.txtNameMenu1, this.txtNameMenu2);
                }
                if (tag == "Age")
                {
                    ClearMenuFilter(this.txtAgeMenu1, this.txtAgeMenu2);
                }
                FilterMenuTask();
            }
        }


        private void btnOkFilter_Click(object sender, RoutedEventArgs e)
        {
            if (sender == null)
            {
                return;
            }
            var btn = sender as System.Windows.Controls.Button;
            if (btn != null)
            {
                var tag = btn.Tag.ToString();
                HidePopupMenu(tag);
                FilterMenuTask();
            }
        }

        /// <summary>
        /// 隱藏父Popup
        /// </summary>
        /// <param name="tag"></param>
        private void HideParentPopup(string tag)
        {
            //點(diǎn)擊右鍵菜單時(shí),隱藏父Popup控件
            if (tag == "No")
            {
                this.popupNo.IsOpen = false;
            }
            if (tag == "Name")
            {
                this.popupName.IsOpen = false;
            }
            if (tag == "Age")
            {
                this.popupAge.IsOpen = false;
            }
        }

        /// <summary>
        /// 隱藏菜單彈出的Popup控件
        /// </summary>
        /// <param name="tag"></param>
        private void HidePopupMenu(string tag)
        {
            var pop = this.FindName($"popup{tag}Menu");
            if (pop != null)
            {
                var popup = pop as System.Windows.Controls.Primitives.Popup;
                popup.IsOpen = false;
            }
        }

        /// <summary>
        /// 清除菜單中的文本過(guò)濾條件
        /// </summary>
        /// <param name="txt1"></param>
        /// <param name="txt2"></param>
        private void ClearMenuFilter(TextBox txt1, TextBox txt2)
        {
            txt1?.Clear();
            txt2?.Clear();
        }

        /// <summary>
        ///
        /// </summary>
        private void FilterMenuTask()
        {
            var cv = CollectionViewSource.GetDefaultView(this.dgStudents.ItemsSource);
            if (cv != null && cv.CanFilter)
            {
                cv.Filter = new Predicate<object>((obj) =>
                {
                    bool flag = true;
                    var t = obj as Student;
                    if (t != null)
                    {
                        string noText1 = this.txtNoMenu1.Text.Trim();
                        string noText2 = this.txtNoMenu2.Text.Trim();
                        int noConditionType1 = this.combNoMenu1.SelectedIndex;
                        int noConditionType2 = this.combNoMenu2.SelectedIndex;
                        string nameText1 = this.txtNameMenu1.Text.Trim();
                        string nameText2 = this.txtNameMenu2.Text.Trim();
                        int nameConditionType1 = this.combNameMenu1.SelectedIndex;
                        int nameConditionType2 = this.combNameMenu2.SelectedIndex;
                        string ageText1 = this.txtAgeMenu1.Text.Trim();
                        string ageText2 = this.txtAgeMenu2.Text.Trim();
                        int ageConditionType1 = this.combAgeMenu1.SelectedIndex;
                        int ageConditionType2 = this.combAgeMenu2.SelectedIndex;
                        bool? isNoAnd = this.rbNoAnd.IsChecked;
                        bool? isNoOr = this.rbNoOr.IsChecked;
                        bool? isNameAnd = this.rbNameAnd.IsChecked;
                        bool? isNameOr = this.rbNameOr.IsChecked;
                        bool? isAgeAnd = this.rbAgeAnd.IsChecked;
                        bool? isAgeOr = this.rbAgeOr.IsChecked;
                        bool flagNo = true;
                        bool flagName = true;
                        bool flagAge = true;
                        flagNo = CheckConditions(noConditionType1, noConditionType2, t.No, noText1, noText2, isNoAnd, isNoOr);
                        flagName = CheckConditions(nameConditionType1, nameConditionType2, t.Name, nameText1, nameText2, isNameAnd, isNameOr);
                        flagAge = CheckConditions(ageConditionType1, ageConditionType2, t.Age.ToString(), ageText1, ageText2, isAgeAnd, isAgeOr);
                        flag = flag && flagNo && flagName && flagAge;
                    }
                    return flag;
                });
            }
        }

        private bool CheckConditions(int conditionIndex1, int conditionIndex2, string source, string condition1, string condition2, bool? isAnd, bool? isOr)
        {
            bool flag = true;
            bool flag1 = true;
            bool flag2 = true;
            if (!string.IsNullOrEmpty(condition1) && !string.IsNullOrWhiteSpace(condition1) && conditionIndex1 != -1)
            {
                flag1 = CheckCondition(conditionIndex1, source, condition1);
            }
            if (!string.IsNullOrEmpty(condition2) && !string.IsNullOrWhiteSpace(condition2) && conditionIndex2 != -1)
            {
                flag2 = CheckCondition(conditionIndex2, source, condition2);
            }
            if (isAnd == true)
            {
                flag = flag1 && flag2;
            }
            if (isOr == true)
            {
                flag = flag1 || flag2;
            }
            return flag;
        }

        private bool CheckCondition(int condtionIndex, string source, string condition)
        {
            bool flag = true;
            if (condtionIndex == 0)
            {
                flag = flag && source == condition;
            }
            if (condtionIndex == 1)
            {
                flag = flag && source != condition;
            }
            if (condtionIndex == 2)
            {
                flag = flag && source.StartsWith(condition);
            }
            if (condtionIndex == 3)
            {
                flag = flag && source.EndsWith(condition);
            }
            if (condtionIndex == 4)
            {
                flag = flag && source.Contains(condition);
            }
            if (condtionIndex == 5)
            {
                flag = flag && !source.Contains(condition);
            }
            return flag;
        }
    }
}

學(xué)號(hào),姓名,年齡三列過(guò)濾列表綁定內(nèi)容模型一致,為FilterInfo,如下所示:

using CommunityToolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DemoDataGrid
{
    public class FilterInfo : ObservableObject
    {
        private string filterText;

        public string FilterText
        {
            get { return filterText; }
            set { SetProperty(ref filterText, value); }
        }

        private bool isChecked;

        public bool IsChecked
        {
            get { return isChecked; }
            set { SetProperty(ref isChecked, value); }
        }
    }
}

不足與思考

上述篩選實(shí)現(xiàn)方式,并非唯一實(shí)現(xiàn),也并非最優(yōu)實(shí)現(xiàn),同樣存在許多可以優(yōu)化的地方。

在本示例中,存在許多冗余代碼,如視圖頁(yè)面,對(duì)三列的彈出窗口,內(nèi)容雖然相對(duì)統(tǒng)一,只是列名和綁定內(nèi)容不同而已,卻堆積了三大段代碼,是否可以從控件模塊或者數(shù)據(jù)模板的角度,進(jìn)行簡(jiǎn)化呢?

篩選功能實(shí)現(xiàn)上,同樣存在許多冗余代碼,是否可以進(jìn)行簡(jiǎn)化呢?以上是我們需要思考的地方,希望可以集思廣益,共同學(xué)習(xí),一起進(jìn)步。

到此這篇關(guān)于WPF實(shí)現(xiàn)帶篩選功能的DataGrid的文章就介紹到這了,更多相關(guān)WPF DataGrid內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C#開(kāi)發(fā)Winform控件之打開(kāi)文件對(duì)話框OpenFileDialog類

    C#開(kāi)發(fā)Winform控件之打開(kāi)文件對(duì)話框OpenFileDialog類

    這篇文章介紹了C#開(kāi)發(fā)Winform控件之打開(kāi)文件對(duì)話框OpenFileDialog類,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-02-02
  • C#裝飾器模式(Decorator Pattern)實(shí)例教程

    C#裝飾器模式(Decorator Pattern)實(shí)例教程

    這篇文章主要介紹了C#裝飾器模式(Decorator Pattern),以一個(gè)完整實(shí)例形式講述了C#裝飾器模式的實(shí)現(xiàn)過(guò)程,有助于深入理解C#程序設(shè)計(jì)思想,需要的朋友可以參考下
    2014-09-09
  • C#實(shí)現(xiàn)讓ListBox適應(yīng)最大Item寬度的方法

    C#實(shí)現(xiàn)讓ListBox適應(yīng)最大Item寬度的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)讓ListBox適應(yīng)最大Item寬度的方法,涉及ListBox控件的操作技巧,需要的朋友可以參考下
    2015-05-05
  • C#中兩個(gè)byte如何相加

    C#中兩個(gè)byte如何相加

    可能有的看到這個(gè)題目就會(huì)覺(jué)得這不簡(jiǎn)單嗎?直接用+號(hào)相加就行了,可是當(dāng)你實(shí)際操作運(yùn)行的時(shí)候就會(huì)發(fā)現(xiàn)有錯(cuò)誤了,那么是什么錯(cuò)誤?那該如何讓C#中兩個(gè)byte相加呢?通過(guò)下面這篇文章來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2016-11-11
  • C#多線程系列之線程等待

    C#多線程系列之線程等待

    本文詳細(xì)講解了C#多線程中的線程等待,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-02-02
  • WPF使用WrapPanel實(shí)現(xiàn)虛擬化效果

    WPF使用WrapPanel實(shí)現(xiàn)虛擬化效果

    這篇文章主要為大家詳細(xì)介紹了如何利用WPF WrapPanel實(shí)現(xiàn)虛擬化效果,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,感興趣的小伙伴可以了解一下
    2022-09-09
  • C#中AS和IS關(guān)鍵字的用法

    C#中AS和IS關(guān)鍵字的用法

    這篇文章主要介紹了C#中AS和IS關(guān)鍵字的用法的相關(guān)資料,需要的朋友可以參考下
    2016-03-03
  • 帶你一文了解C#中的LINQ

    帶你一文了解C#中的LINQ

    c#提供的ling查詢極大的遍歷了集合的查詢過(guò)程,且使用簡(jiǎn)單方便,非常的有用,下面這篇文章主要給大家介紹了關(guān)于C#中LINQ的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-12-12
  • C# 構(gòu)造函數(shù)如何調(diào)用虛方法

    C# 構(gòu)造函數(shù)如何調(diào)用虛方法

    這篇文章主要介紹了C# 構(gòu)造函數(shù)如何調(diào)用虛方法,文中講解非常詳細(xì),示例代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • 高效C#編碼優(yōu)化原則

    高效C#編碼優(yōu)化原則

    這篇文章主要介紹了高效C#編碼優(yōu)化原則,非常實(shí)用,需要的朋友可以參考下
    2014-08-08

最新評(píng)論