1、前台页面的代码,TextBox下面放置了一个Popup控件。当TextBox获得焦点时,下面的Popup控件就会显示出来,并且根据TextBox的内容进行了模糊查询
1 <UserControl x:Class="PopListView.PopView" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 mc:Ignorable="d"> 7 8 <Grid x:Name="LayoutRoot" Background="White" > 9 <Grid.RowDefinitions> 10 <RowDefinition Height="Auto" /> 11 <RowDefinition Height="Auto"/> 12 </Grid.RowDefinitions> 13 <TextBox x:Name="txtUserName" LostFocus="txtUserName_LostFocus" ></TextBox> 14 <Popup x:Name="popupAutoComplte" Grid.Row="1"> 15 <Grid Background="Gray" > 16 <ListBox x:Name="SelctName" SelectedItem="{Binding SelectStr,Mode=TwoWay}" 17 18 ItemsSource="{Binding LoginNameList,Mode=TwoWay}"></ListBox> 19 </Grid> 20 </Popup> 21 </Grid> 22 </UserControl>
2、后台代码
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Net; 5 using System.Windows; 6 using System.Windows.Controls; 7 using System.Windows.Documents; 8 using System.Windows.Input; 9 using System.Windows.Media; 10 using System.Windows.Media.Animation; 11 using System.Windows.Shapes; 12 13 namespace PopListView 14 { 15 16 public partial class PopView : UserControl 17 { 18 public PopView() 19 { 20 InitializeComponent(); 21 bool _isEnterPress = true; 22 txtUserName.SelectionChanged += new RoutedEventHandler((s, e) => 23 { 24 popupAutoComplte.IsOpen = false; 25 if (LoginNameList != null && LoginNameList.Count() > 0) 26 { 27 SelctName.Items.Clear(); 28 TextBox txtbox = s as TextBox; 29 if (txtbox.Text.Trim() != "") 30 { 31 if (_isEnterPress) 32 popupAutoComplte.IsOpen = true; 33 34 LoginNameList = from u in LoginNameList 35 where u.Contains(txtbox.Text) 36 select u; 37 38 foreach (var item in LoginNameList) 39 { 40 SelctName.Items.Add(item); 41 } 42 _isEnterPress = true; 43 } 44 else 45 { 46 47 } 48 } 49 }); 50 51 SelctName.SelectionChanged += new SelectionChangedEventHandler((s, e) => 52 { 53 ListBox Box = (ListBox)s; 54 if (Box.SelectedItem != null) 55 { 56 txtUserName.Text = Box.SelectedItem.ToString(); 57 if (CheckLoginName != null) 58 { 59 CheckLoginName(txtUserName.Text); 60 } 61 _isEnterPress = false; 62 popupAutoComplte.IsOpen = false; 63 } 64 }); 65 this.Loaded += new RoutedEventHandler(PropertyControl_Loaded); 66 } 67 68 void PropertyControl_Loaded(object sender, RoutedEventArgs e) 69 { 70 71 } 72 73 #region 自定义的变量 74 75 public IEnumerable<string> LoginNameList 76 { 77 get { return (IEnumerable<string>)GetValue(LoginNameListProperty); } 78 set { SetValue(LoginNameListProperty, value); } 79 } 80 81 // Using a DependencyProperty as the backing store for LoginNameList. This enables animation, styling, binding, etc... 82 public static readonly DependencyProperty LoginNameListProperty = 83 DependencyProperty.Register("LoginNameList", typeof(IEnumerable<string>), typeof(PopView), new PropertyMetadata(OnChange)); 84 85 private static void OnChange(DependencyObject d, DependencyPropertyChangedEventArgs e) 86 { 87 88 } 89 public String Text 90 { 91 get { return txtUserName.Text; } 92 } 93 94 95 96 public double PopWidth 97 { 98 get { return (double)GetValue(PopWidthProperty); } 99 set { SetValue(PopWidthProperty, value); } 100 } 101 102 // Using a DependencyProperty as the backing store for PopWidth. This enables animation, styling, binding, etc... 103 public static readonly DependencyProperty PopWidthProperty = 104 DependencyProperty.Register("PopWidth", typeof(double), typeof(PopView), new PropertyMetadata(new PropertyChangedCallback((n, s) => 105 { 106 ((PopView)n).txtUserName.Width = double.Parse(s.NewValue.ToString()); 107 ((PopView)n).SelctName.Width = double.Parse(s.NewValue.ToString()); 108 }))); 109 110 public delegate void LoginName(string Name); 111 public event LoginName CheckLoginName; 112 #endregion 113 114 private void txtUserName_LostFocus(object sender, RoutedEventArgs e) 115 { 116 popupAutoComplte.IsOpen = false; 117 118 } 119 120 121 } 122 }
这个本来在项目中我是使用在登陆的时候用到了。而且用到了独立储存空间来保存用户的登陆信息;