У меня есть autocompletebox из Wpf Toolkit в моей сетке данных wpf. Ниже мой xaml:
<DataGridTemplateColumn Header="Account Type">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<toolkit:AutoCompleteBox Text="{Binding Path='Account Type'}" Populating="PopulateAccountTypesACB" IsTextCompletionEnabled="True" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
В моем событии Заполнение я хочу установить источник элементов на основе запроса, который я выполняю. Вот что у меня есть для этого:
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 в этой функции? Я попытался присвоить имя автозаполнению и использовать его из функции, но не смог получить к нему доступ.