How to use AutoCompleteBox with MVVM Light?

Jun 4, 2013 at 10:40 PM
Hi

I am trying to figure out how the best way to use AutoCompleteBox with MVVM Light.
     public ICommand AutoComplete
        {
            get
            {
                return new RelayCommand<KeyEventArgs>(e =>
                {
                    var txtBox = e.OriginalSource as TextBox;

                    if (e.Key == Key.Unknown)
                    {
                        return;
                    }

                    string autoComplete = txtBox.Text + e.Key;

                    if (autoComplete.Length >= 3)
                    {
                        RestClient c = new RestClient("http://localhost:3333/api/store");
                        RestRequest r = new RestRequest("/GetStoreNames",Method.GET);
                        r.AddParameter("Name", autoComplete);
                        r.AddParameter("Latitude", "49");
                        r.AddParameter("Longitude", "49");
                        var d =  c.BuildUri(r);
                        c.ExecuteAsync(r, response2 =>
                          {
                              var content = response2.Content;
                          });
                    }


                });
            }
        }


    <i:Interaction.Triggers>
                <i:EventTrigger EventName="KeyUp">
                    <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding AutoComplete, Mode=OneWay}" PassEventArgsToCommand="True"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>


<toolkit:AutoCompleteBox x:Name="acbStore" Margin="154,196,29,0" VerticalAlignment="Top" RenderTransformOrigin="0.6,0.083" Height="162" MinimumPopulateDelay="500"/>
I did the above but there is a couple problems.
  1. Once I get the results back how do I show them in the auto complete area?
  2. How can I delay it from doing to many requests at once. As you can see I don't want to hit the server before 3 characters are entered but after that it is fair game. I am kinda worried that like 20 requests will be done to the server before the 1st request makes it back leading to wasted bandwidth.
  3. For some reason it seems like all my textboxes get the ICommand Event Trigger and I don't understand why. I click in another box and start typing and starts hitting my ICommand.
Coordinator
Jun 4, 2013 at 11:35 PM
Hi,

I would recommend to ask on StackOverflow, with the mvvm-light tag. There is a lot more traffic there and you will increase your chances to find someone having actually done this before :)

Cheers
Laurent
Jun 5, 2013 at 2:36 AM
lbugnion wrote:
Hi,

I would recommend to ask on StackOverflow, with the mvvm-light tag. There is a lot more traffic there and you will increase your chances to find someone having actually done this before :)

Cheers
Laurent
I usually do post on stack but I find actually I get more answers here.
Coordinator
Jun 5, 2013 at 7:50 AM
Interesting! Thanks for the note :)

Laurent