【问题标题】:WPF Combobox updating but not Display show when ComboBox ItemsSource changed当 ComboBox ItemsSource 更改时,WPF Combobox 更新但不显示显示
【发布时间】:2019-09-11 13:39:22
【问题描述】:

我是 WPF 新手,正在尝试修复 keyup 事件的自动建议组合框

Xaml 代码:这是我的 xaml:

<ComboBox x:Name="party_list" Margin="10,15,0,6" Grid.Column="1" AllowDrop="True" IsTextSearchEnabled="False" IsEditable="True" KeyUp="party_list_KeyUp" >

C# 代码:背后的代码


    public partial class page_addsale : Page
    {

        List<string> nameList { get; set; }
        DataTable data = new DataTable();
        List<string> autoList = new List<string>();

        public page_addsale()
        {
            InitializeComponent();

            nameList = new List<string>();

            con.Open();
            OleDbDataAdapter ad = new OleDbDataAdapter("select id, party_name from party_list", con);
            ad.Fill(data);
            con.Close();

            party_list.ItemsSource= data.DefaultView;
            party_list.DisplayMemberPath = "party_name";
            party_list.SelectedValuePath = "id";

            string[] arr = data.AsEnumerable().Select<System.Data.DataRow, String>(x => x.Field<String>("party_name")).ToArray();
            nameList.AddRange(arr);            

        }       

        private void party_list_KeyUp(object sender, KeyEventArgs e)
        {
            party_list.ItemsSource = null;
            var names = from n in nameList where (n.StartsWith(party_list.Text)) select n;

            foreach (string name in names)
            {
                autoList.Add(name.ToString());
            }
            try
            {
                if (party_list.Text.Length > 0)
                {
                   if (autoList.Count > 0)
                   {
                        party_list.ItemsSource = autoList;
                        party_list.IsDropDownOpen = true;

                   }                  
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

当 itemssource 绑定改变时项目更新的位置 但不显示在组合框中

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    分配新的party_list.ItemsSource = autoList;之前需要先清除

    party_list.DisplayMemberPath = "";
    party_list.SelectedValuePath = "";
    

    但我不会建议这样创建类。并将您的数据表转换为其类。让你有恒心。

    【讨论】:

    • @VashviVekariya Np 如果工作,请尝试接受答案,一段时间后我会给出一些恒常性的例子
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    • 2017-03-07
    • 2014-01-26
    • 2022-11-19
    相关资源
    最近更新 更多