AutoComplete Textbox Text

Oct 9, 2013 at 6:26 PM
Edited Oct 9, 2013 at 6:27 PM
I am able to incorporate the AutoComplete feature in a test program but the TextBox.Text value is not being set/updated correctly. The various companies in CompanyList do show correctly in the drop-down list and if I select a company it gets set to the SelectedObject variable, but the company name is not displayed in the TextBox.

Any idea what I am doing wrong? I have tried binding the TextBox.Text to SelectedObject and SelectedObject.Name but nothing populates int eh TextBox.Text field.

In case it matters I am using MVVM Light (hence the ViewModelBase).

Here is my code:
    public class MainViewModel : ViewModelBase
    {
            _companyList = new ObservableCollection<Company>();
            _companyList.Add(new Company(1, "Company A", "123 Main Street", "Columbus"));
            _companyList.Add(new Company(2, "Company B", "456 Main Street", "Columbus"));
            _companyList.Add(new Company(3, "Company C", "789 Main Street", "Columbus"));
            _companyList.Add(new Company(4, "Company B", "111 Main Street", "Columbus"));
            _companyList.Add(new Company(5, "Company B", "222 Main Street", "Columbus"));
            _companyList.Add(new Company(6, "Company C", "456 Main Street", "Columbus"));
        }

        private const string CompanyListPropertyName = "CompanyList";
        private ObservableCollection<Company> _companyList;
        public ObservableCollection<Company> CompanyList
        {
            get
            {
                return _companyList;
            }
            set
            {
                if (_companyList != value)
                {
                   _companyList = value;
                   RaisePropertyChanged(CompanyListPropertyName);
               }
            }

        }

        private Company _selectedObject;
        public Company SelectedObject
        {
            get { return _selectedObject; }
            set
            {
                if (_selectedObject != value)
                {
                    _selectedObject = value;
                    
                }
            }
        }
    }
<Window x:Class="MvvmLight2.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:code="clr-namespace:CODE.Framework.Wpf.Controls;assembly=CODE.Framework.Wpf"
        xmlns:ignore="http://www.ignore.com"
        mc:Ignorable="d ignore"
        Height="374"
        Width="404"
        Title="MVVM Light Application"
        DataContext="{Binding Main, Source={StaticResource Locator}}">

    <Grid x:Name="LayoutRoot">

        <TextBlock FontSize="36"
                   FontWeight="Bold"
                   Foreground="Purple"
                   Text="Welcome"
                   VerticalAlignment="Center"
                   HorizontalAlignment="Center"
                   TextAlignment="Center"
                   TextWrapping="Wrap" Margin="77,10,94,283" Height="50" Width="225" />

        <TextBox Name="OmniSearchTextBox"
                 Text="{Binding SelectedObject, UpdateSourceTrigger=PropertyChanged}" 
                 code:AutoComplete.AutoCompleteItemsSource="{Binding CompanyList}" 
                 code:AutoComplete.AutoCompleteDisplayMemberPath="Name"
                 code:AutoComplete.AutoCompleteSelectedItem="{Binding SelectedObject, Mode=TwoWay}"
                 VerticalAlignment="Top" 
                 Margin="10,94,10,0" />

    </Grid>
</Window>
    public class Company
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public string City { get; set; }


        public Company()
        {
            this.ID = -1;
            this.Name = "";
            this.Address = "";
            this.City = "";
        }
        public Company(int id, string name, string address, string city)
        {
            this.ID = id;
            this.Name = name;
            this.Address = address;
            this.City = city;
        }

        public override string ToString()
        {
            return Name;
        }
    }
Developer
Oct 10, 2013 at 1:07 PM
Hello Brian,
I think the issues is that you are binding the TextBox and the AutoCompleteSelectedItem to the same object (SelectedObject). Try changing
Text="{Binding SelectedObject, UpdateSourceTrigger=PropertyChanged}" 

to

Text="{Binding SearchString, UpdateSourceTrigger=PropertyChanged}" 
then on the setter of the SeachString property perform code to filter the CompanyList.

Jeff