My Master's Thesis Problems and solutions encountered…

11May/100

AutoCompleteBox

When inserting a search function within your application, it is useful to use the AutoCompleteBox if you already know what the user will search for. In the case of my application, the user can only search for a specific set of company names, and so the AutoCompleteBox is useful.

Like in the previous post, I will hardcode the set data into the code, but this will later be changed so it collects the names from a MySQL database.

First, you need to create a reference to System.Windows.Controls and to System.Windows.Controls.Input. You do this by right-clicking on your project >> Add reference >> Choose System.Windows.Controls, and System.Windows.Controls.Input. If you do not add the reference, you'll be presented with an error message saying "Verify that you are not missing an assembly reference".

Next, you need to add a an xmlns entry at the top of your .xaml page.


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

Then you just insert your new AutoCompleteBox. Something like this (notice the IsTextCompletionEnabled):


  <input:AutoCompleteBox x:Name="Supplier"
                          Width="220"
                          Height="20"
                          Margin="10"
                          IsTextCompletionEnabled ="True"/>

The IsTextCompletionEnabled enables the automatic completion of the text as you type. Its default is set to False, so if you do not wish it to auto-complete, then do not add the property.

In the code-behind, you can bind a simple string array to the ItemsSource property:


 public Customer_Find_Supplier()
        {
            InitializeComponent();
            this.Supplier.ItemsSource = new string[]
            {
            "Company One", "Company Two", "Company three"
            };
        }

And that's it!

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


8 − seven =

No trackbacks yet.