动态设置wpf工具箱Autocompletebox Itemsource
本文关键字:Autocompletebox Itemsource 工具箱 wpf 设置 动态 | 更新日期: 2023-09-27 18:16:22
在我的Wpf数据网格中有一个来自Wpf工具箱的自动完成框。下面是我的xaml:
<DataGridTemplateColumn Header="Account Type">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<toolkit:AutoCompleteBox Text="{Binding Path='Account Type'}" Populating="PopulateAccountTypesACB" IsTextCompletionEnabled="True" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
在我的populate事件中,我希望根据我正在运行的查询设置项目源。下面是我到目前为止所做的:
private void PopulateAccountTypesACB(object sender, PopulatingEventArgs e)
{
try
{
List<string> types = new List<string>();
string accountQuery = "SELECT AccountType FROM AccountType WHERE AccountType LIKE '" + e.Parameter +"%'";
SqlDataReader accountTypes = null;
SqlCommand query = new SqlCommand(accountQuery, dbConnection);
accountTypes = query.ExecuteReader();
while (accountTypes.Read())
{
types.Add(accountTypes["AccountType"].ToString());
}
accountTypes.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
// Close the DB if there was an error.
if (dbConnection.State == ConnectionState.Open)
dbConnection.Close();
}
}
如何在这个函数中设置ItemSource ?我试着给autocompletebox分配一个名字,并从函数中使用它,但我无法从那里访问它。
我不认为这是一个好主意-在事件处理程序中执行搜索查询,但要设置ItemSource
,只需将发送方转换为AutoCompleteBox
:
AutoCompleteBox accountType = (AutoCompleteBox)sender;
accountType.ItemSource = types;