zoukankan      html  css  js  c++  java
  • wpf 可筛选下拉框

    using System.Collections.Generic;
    using System.Windows;
    using System.Windows.Controls.Primitives;
    using System.Windows.Input;
    
    namespace WpfApp25
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
    
                var nameList = new List<string>
                {
                    "A0-Word","B0-Word","C0-Word",
                    "A1-Word","A111","A11122","B1-Word","C1-Word",
                    "B2-Word","C2-Word",
                    "C3-Word",
                };
    
                comboBox1.DataContext = nameList;
                comboBox1.SelectedValue = "aaa";
                comboBox1.Loaded += delegate
                {
                    System.Windows.Controls.TextBox textBox = comboBox1.Template.FindName("PART_EditableTextBox", comboBox1) as System.Windows.Controls.TextBox;
                    Popup popup = comboBox1.Template.FindName("PART_Popup", comboBox1) as Popup;
                    if (textBox != null)
                    {
                        textBox.KeyUp += (s, e) =>
                        {
                            if (e.Key == Key.Down)
                            {
                                if (comboBox1.SelectedIndex==comboBox1.Items.Count-1)
                                {
                                    return;
                                }
                                comboBox1.SelectedIndex += 1;
                                return;
                            }
                            else if (e.Key==Key.Up)
                            {
                                if (comboBox1.SelectedIndex==-1)
                                {
                                    return;
                                } 
                                comboBox1.SelectedIndex -= 1;
                                return;
                            }
                            comboBox1.Items.Filter += (item) =>
                            {  string unSelected = textBox.Text;
                                if (!string.IsNullOrEmpty(textBox.SelectedText) )
                                {
                                  unSelected = textBox.Text.Replace(textBox.SelectedText, "");
                                } 
                                if (item.ToString().ToLower().Contains(unSelected.ToLower()))
                                {
                                    popup.IsOpen = true;
                                    return true;
    
                                }
                                else
                                {
                                    return false;
                                }
                            };
                        };
                    }
                };
    
                //  comboBox1.KeyDown += new System.Windows.Input.KeyEventHandler(comboBox1_KeyDown);
    
            }
    
            private void comboBox1_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
            {
    
                if (e.Key == Key.Down)
                {
                    e.Handled = true;
    
                }
            }
        }
    }
    <Window x:Class="WpfApp25.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:WpfApp25"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800">
        <Grid>
            <ComboBox Name="comboBox1"
                      IsEditable="true"
                      Width="200"
                      Height="50"
                      ItemsSource="{Binding}"> 
            </ComboBox>
        </Grid>
    </Window>
  • 相关阅读:
    创业失败的七个原因及解决之道
    技术人员如何参与产品设计讨论:激活那一潭死水
    基于Android Studio搭建hello world工程
    WINCE6.0+IMX515通过cfimager.exe烧录镜像文件
    基于Android Studio搭建Android应用开发环境
    WinCE启动失败的原因与解决办法分析
    Maximum Allowed Error 7 错误解决
    s3c6410 开发板Linux系统支持 K9GAG08U0E的方法
    Nandflash 驱动移植
    GIFT-EMS礼记----青软S2SH(笔记)
  • 原文地址:https://www.cnblogs.com/congqiandehoulai/p/12721514.html
Copyright © 2011-2022 走看看