动态设置wpf工具包Autocompletebox Itemssource

时间:2011-12-01 03:38:14

标签: c# wpf datagrid autocomplete

我的wpf数据网格中有一个来自Wpf Toolkit的自动完成框。以下是我的xaml:

                    <DataGridTemplateColumn Header="Account Type">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <toolkit:AutoCompleteBox Text="{Binding Path='Account Type'}"   Populating="PopulateAccountTypesACB" IsTextCompletionEnabled="True" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

在我的Populating事件中,我想根据我正在运行的查询设置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?我尝试为自动完成框分配一个名称并使用该功能,但我无法从那里访问它。

1 个答案:

答案 0 :(得分:1)

我不确定这是一个好主意 - 在事件处理程序中执行搜索查询,但设置ItemSource只是将发件人强制转换为AutoCompleteBox

AutoCompleteBox accountType = (AutoCompleteBox)sender;
accountType.ItemSource = types;
相关问题