Rinktacular Rinktacular - 1 month ago 35
C# Question

Trouble Setting Up "Item Source" for AutoCompleteTextBox from WPF Extended Toolkit

I know that in the the base version of WPF, you can set up a combo box with an ItemSource for options in the drop down:

ComboBox box = new ComboBox();
box.ItemSource = List<string> exampleList;


I am using the WPF Extended Toolkit to attempt that same feature with the AutoCompleteTextBox, however there is no "Item Source" Property. I have been looking at examples online for the past few days but have yet to find an example where the suggestion list is generated. I have properly set up my XAML to include the ToolKit namespace and the actual textbox shows up fine:

<Window x:Class="complianceAuthApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:complianceAuthApp"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
mc:Ignorable="d"
Title="Authorization Submission App" Height="290" Width="747" Closing="Window_Closing" ResizeMode="CanMinimize">
<grid>
...
<xctk:AutoSelectTextBox
Name="lastNameAutoTextBox"
AutoWordSelection="True"
BorderBrush="DarkGray"
AutoSelectBehavior="OnFocus"
Margin="21,35,586,193"/>
</Grid>
</Window>


However, I want to run a SQL query whenever the number of characters in the textbox reaches 3 or more:

public void getAutoTextBoxItemSource()
{

if (lastNameAutoTextBox.Text.Length < 3)
return;

List<string> results = new List<string>();
SqlConnection cnn = new SqlConnection(ConfigurationManager["SQLQuery"].ConnectionString);
string sqlCommand = "SELECT TOP 10 ... ";

cnn.Open();
SQL.DataSet ds = new SQL.DataSet();
SQL.DataTable dtable = new SQL.DataTable();
SqlDataAdapter dscmd = new SqlDataAdapter(sqlCommand, cnn);


int t = await Task.Run(() => dscmd.Fill(dtable));
foreach (SQL.DataRow row in dtable.Rows)
{
results.Add(new string);
}

//If there WERE to be an ItemSource Property...
lastNameAutoTextBox.ItemSource=results;

}
}


I can not seem to find an example that fits my needs and have spend the past few days trying to figure out how to implement my project in this way.

Answer

The control in your XAML is <xctk:AutoSelectTextBox. Xceed.Wpf.Toolkit.AutoSelectTextBox is a textbox which selects its text automatically when the user tabs into it. It's not an auto-complete textbox.

You need a different WPF Toolkit. Go to the Downloads page, download and install WPFToolkit.msi. It installs a raft of assemblies in C:\Program Files (x86)\WPF Toolkit\v3.5.50211.1\. So right click on "References" in your WPF project, select "Add Reference". In the dialog, click "Browse...", and browse to C:\Program Files (x86)\WPF Toolkit\v3.5.50211.1\. Select these two assemblies:

System.Windows.Controls.Input.Toolkit.dll
WPFToolkit.dll

And Add them.

Then add this namespace to your XAML:

xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"

And you should be able to use toolkit:AutoCompleteBox

<toolkit:AutoCompleteBox
    ItemsSource="{Binding Items}"
    />

There are other answers about this, but as far as I can tell they're incomplete and/or obsolete. I couldn't find one that said where the MSI puts the assemblies. One says you'll find a "Data Visualization" section in your Toolbox panel in VS. That may have been the case at some point, but it is not the case in VS 2015 with the version I just installed. YMMV, maybe.