0
我有我的wpf數據網格中Wpf工具包的自動完成框。下面是我的xaml:動態設置wpf工具包Autocompletebox Itemsource
<DataGridTemplateColumn Header="Account Type">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<toolkit:AutoCompleteBox Text="{Binding Path='Account Type'}" Populating="PopulateAccountTypesACB" IsTextCompletionEnabled="True" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
在我的填充事件中,我想根據我正在運行的查詢設置itemsource。以下是我目前爲此所做的工作:
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分配一個名稱並使用它,但是我無法從那裏訪問它。
這是爲我工作!在事件處理程序中執行查詢有什麼缺點? –