我是 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);
}
}
当项目源绑定更改时项目正在更新但不显示在组合框中
万千封印
相关分类