本文介绍了属性网格:自动完成组合框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何通过从StringConverter派生在PropertyGrid中创建下拉列表.我也知道,如果我对GetStandardValuesExclusive()的覆盖返回True,则该下拉列表实际上是一个简单的下拉列表,您不能键入它,只能从中选择.如果返回False,则可以键入,但现在将不再验证.

我的问题是,是否有一种简单的方法可以强制PropertyGrid中的下拉列表强制(A)自动完成,并且(B)将条目限制为列表(验证条目)?

I know how to create dropdowns in a PropertyGrid by deriving from StringConverter. I also know that if my override of GetStandardValuesExclusive() returns True, then the dropdown is in fact a simple dropdown and you can't type into it but only select from it. If it returns False, you can type into it but now it will no longer validate.

My question is, is there an easy way to force the dropdown inside the PropertyGrid to (A) autocomplete and (B) limit the entry to the list (validate the entry)?

推荐答案

<ComboBox Name="cmbCustomer" IsEditable="True" IsReadOnly="True" IsTextSearchEnabled="False"/>


C#:


C#:

        public SelectCustomer()
        {
            InitializeComponent();
            cmbCustomer.ItemsSource = LoadCustomerList();
            cmbCustomer.Loaded += new RoutedEventHandler(cmbCustomer_Loaded);
            cmbCustomer.DropDownClosed += new EventHandler(cmbCustomer_DropDownClosed);
        }

        void cmbCustomer_DropDownClosed(object sender , EventArgs e)
        {
            ///Clear the combobox if what the users types in is not in the list
            if (cmbCustomer.Text != "")
            {
                TextBox textBox = cmbCustomer.Template.FindName("PART_EditableTextBox" , cmbCustomer) as TextBox;

                if (!cmbCustomer.Items.Contains(textBox.Text))
                {
                    textBox.Clear();
                }
            }
        }

        private IList<string> LoadCustomerList()
        {
            ///get the list of customers from the database
            DataTable dt_Customer = AppMain.dataAccess.getApprovedCustomers();
            List<string> customers = new List<string>();

            foreach (DataRow dr in dt_Customer.Rows)
            {
                customers.Add(dr["Customers"].ToString());
            }

            return customers;
        }

        void cmbCustomer_Loaded(object sender , RoutedEventArgs e)
        {
            ///only necessary if the FindName statement bellow returns null
            cmbCustomer.ApplyTemplate();

            TextBox textBox = cmbCustomer.Template.FindName("PART_EditableTextBox" , cmbCustomer) as TextBox;
            textBox.IsReadOnly = false;

            if (textBox != null)
            {
                textBox.TextChanged += delegate
                {
                    ///open the drop down and start filtering based on what the user types into the combobox
                    cmbCustomer.IsDropDownOpen = true;
                    cmbCustomer.Items.Filter += a =>
                    {
                        if (a.ToString().ToUpper().Contains(textBox.Text.ToUpper()))
                            return true;
                        else
                            return false;
                    }
                }
            }
        }


这篇关于属性网格:自动完成组合框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:31