<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
WPF 簡單實現下拉篩選控制元件
框架使用.NET40
;
Visual Studio 2022
;
使用 ICollectionView[2] 實現篩選功能,還支援其他如下:
實現程式碼
1)CheckedSearch.cs
程式碼如下:
SearchText
用來記錄輸入的篩選內容Text
用來記錄展示的所選內容^
拼接ItemsSource
資料來源ContainsFilter
篩選資料,如果從資料來源中找到則返回True
using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using WpfCustomControlLibrary1.Datas; namespace WpfCustomControlLibrary1 { public class CheckedSearch : Control { private ICollectionView _filteredCollection; public ICollectionView FilteredCollection { get { return _filteredCollection; } } private string _searchText = string.Empty; public string SearchText { get { return _searchText; } set { if (_searchText != value) { _searchText = value; _filteredCollection.Refresh(); } } } public string Text { get { return (string)GetValue(TextProperty); } set { SetValue(TextProperty, value); } } public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(CheckedSearch), new PropertyMetadata(string.Empty)); public ObservableCollection<CheckedSearchItem> ItemsSource { get { return (ObservableCollection<CheckedSearchItem>)GetValue(ItemsSourceProperty); } set { SetValue(ItemsSourceProperty, value); } } public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(ObservableCollection<CheckedSearchItem>), typeof(CheckedSearch), new PropertyMetadata(null, OnItemsSourceChanged)); private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var choseSearch = (CheckedSearch)d; if (choseSearch == null) return; if (choseSearch._filteredCollection == null && choseSearch.ItemsSource.Count > 0) { foreach (var item in choseSearch.ItemsSource) { item.PropertyChanged -= choseSearch.Item_PropertyChanged; item.PropertyChanged += choseSearch.Item_PropertyChanged; } choseSearch._filteredCollection = CollectionViewSource.GetDefaultView(choseSearch.ItemsSource); choseSearch._filteredCollection.Filter = choseSearch.ContainsFilter; } } string GetItems() { var list = ItemsSource.Where(x=>x.IsChecked).Select(x=>x.Name).ToList(); var visibleItems = string.Join("^",list); return visibleItems; } static CheckedSearch() { DefaultStyleKeyProperty.OverrideMetadata(typeof(CheckedSearch), new FrameworkPropertyMetadata(typeof(CheckedSearch))); } private void Item_PropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == "IsChecked") Text = GetItems(); } private bool ContainsFilter(object item) { var model = item as CheckedSearchItem; if (model == null) return false; if (string.IsNullOrEmpty(SearchText)) return true; if (model.Name.ToUpperInvariant().Contains(SearchText.ToUpperInvariant())) return true; return false; } } }
2)CheckedSearchItem.cs
程式碼如下:
IsChecked
記錄是否選中Name
展示的名稱using System.ComponentModel; namespace WpfCustomControlLibrary1.Datas { public class CheckedSearchItem { public string Name { get; set; } private bool _isChecked; public bool IsChecked { get { return _isChecked; } set { _isChecked = value; OnPropertyChanged("IsChecked"); } } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } }
3)CheckedSearch.xaml
程式碼如下:
<Style TargetType="{x:Type local:CheckedSearch}"> <Setter Property="Height" Value="40"/> <Setter Property="Width" Value="200"/> <Setter Property="BorderBrush" Value="DodgerBlue"/> <Setter Property="Background" Value="White"/> <Setter Property="BorderThickness" Value="1"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:CheckedSearch}"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" x:Name="PART_Border"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <TextBlock Text="{Binding Text,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Foreground="Black" VerticalAlignment="Center" Margin="2,0"/> <ToggleButton x:Name="PART_ToggleButton" Focusable="False" Width="30" Style="{x:Null}" Grid.Column="1" ClickMode="Release"> <Path Stretch="Fill" Height="6" Width="10" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M998 352c0 -8 -4 -17 -10 -23l-50 -50c-6 -6 -14 -10 -23 -10c-8 0 -17 4 -23 10l-393 393l-393 -393c-6 -6 -15 -10 -23 -10s-17 4 -23 10l-50 50c-6 6 -10 15 -10 23s4 17 10 23l466 466c6 6 15 10 23 10s17 -4 23 -10l466 -466c6 -6 10 -15 10 -23z" Fill="Black"> </Path> </ToggleButton> <Popup IsOpen="{Binding ElementName=PART_ToggleButton,Path=IsChecked}" x:Name="PART_Popup" VerticalOffset="2" AllowsTransparency="True" PlacementTarget="{Binding ElementName=PART_Border}" Placement="Bottom" StaysOpen="False"> <Border Width="{TemplateBinding Width}" Padding="2,4" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <StackPanel> <TextBox Text="{Binding SearchText,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Height="40" VerticalContentAlignment="Center" Margin="0,0,0,2"/> <ListBox ItemsSource="{TemplateBinding ItemsSource}"> <ListBox.ItemTemplate> <DataTemplate> <CheckBox Content="{Binding Name}" IsChecked="{Binding IsChecked}"/> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </StackPanel> </Border> </Popup> </Grid> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style>
4)CheckedSearchExample.xaml
範例程式碼如下:
<wd:Window x:Class="WpfApp1.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:wd="https://github.com/WPFDevelopersOrg/WPFDevelopers" xmlns:custom="clr-namespace:WpfCustomControlLibrary1;assembly=WpfCustomControlLibrary1" xmlns:local="clr-namespace:WpfApp1" mc:Ignorable="d" Title="WPFDevelopers - 搜尋多選控制元件" Height="450" Width="800"> <Window.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:,,,/WpfApp1;component/CheckedSearch.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Window.Resources> <!--公眾號:WPF開發者--> <Grid> <custom:CheckedSearch ItemsSource="{Binding ItemsSource,RelativeSource={RelativeSource AncestorType=Window}}" VerticalAlignment="Top" Margin="0,10"/> </Grid> </wd:Window>
5)CheckedSearchExample.xaml
資料來源範例程式碼如下:
using System.Collections.ObjectModel; using System.Windows; using WpfCustomControlLibrary1.Datas; namespace WpfApp1 { public partial class MainWindow { public ObservableCollection<CheckedSearchItem> ItemsSource { get { return (ObservableCollection<CheckedSearchItem>)GetValue(ItemsSourceProperty); } set { SetValue(ItemsSourceProperty, value); } } public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(ObservableCollection<CheckedSearchItem>), typeof(MainWindow), new PropertyMetadata(null)); public MainWindow() { InitializeComponent(); Loaded += MainWindow_Loaded; } private void MainWindow_Loaded(object sender, RoutedEventArgs e) { ItemsSource = new ObservableCollection<CheckedSearchItem>(); var items = new ObservableCollection<CheckedSearchItem>(); items.Add(new CheckedSearchItem { Name = "Winform" }); items.Add(new CheckedSearchItem { Name = "WPF" }); items.Add(new CheckedSearchItem { Name = "WinUI 3" }); items.Add(new CheckedSearchItem { Name = "MAUI" }); items.Add(new CheckedSearchItem { Name = "Avalonia UI" }); ItemsSource = items; } } }
到此這篇關於基於WPF實現簡單的下拉篩選控制元件的文章就介紹到這了,更多相關WPF下拉篩選控制元件內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45