我是 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); } }當項目源綁定更改時項目正在更新但不顯示在組合框中
1 回答

萬千封印
TA貢獻1891條經驗 獲得超3個贊
您需要在分配新的之前清除 party_list.ItemsSource = autoList;
party_list.DisplayMemberPath = "";
party_list.SelectedValuePath = "";
但我不會建議這樣創建類。并將您的數據表轉換為它的類。這樣你就會有恒心。
- 1 回答
- 0 關注
- 401 瀏覽
添加回答
舉報
0/150
提交
取消