This is a migrated thread and some comments may be shown as answers.

How to bind to SelectedItems

3 Answers 533 Views
AutoCompleteBox
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 01 Nov 2012, 09:22 PM
Hello,

I am trying to two-way bind to a List<Stores> in order to pre-populate the auto suggest box and get its updated values. I tried using the following:

<telerik:RadAutoCompleteBox Name="radAutoCompleteBox1" Grid.Column="2" Grid.Row="0"
                            DataContext="{Binding DetailViewModel}"
                            SelectionMode="Multiple"
                            WatermarkContent="Type a name"
                            TextSearchMode="Contains"
                            ItemsSource="{Binding BookstoreLookup}"
                            DisplayMemberPath="ShortName"
                            TextSearchPath="ShortName"
                            AutoCompleteMode="Suggest"
                            SelectedItems="{Binding SelectedNoticeItem.Bookstores, Mode=TwoWay}"
                            />


However, I keep getting "Collection was of a fixed size" exceptions. I've tried no Mode too.

BookstoreLookup is an ObservableCollection<BookstoreModel> and SelectedNoticeItem.Bookstores is a List<BookstoreModel> (I also tried ObservableCollection).

Thanks for your help.

John

3 Answers, 1 is accepted

Sort by
0
Vladi
Telerik team
answered on 02 Nov 2012, 01:59 PM
Hi John,

It seems that the binding of the SelectedItems is causing the control to throw an exception.

The next code snippets show how to bind the SelectedItems to an collection of objects:

in the ViewModel:
public class ViewModel
{
    public ObservableCollection<Country> Countries { get; set; }
    public ObservableCollection<Country> SelectedCountries { get; set; }
 
    public ViewModel()
    {
        this.Countries = new ObservableCollection<Country>()
        {
            new Country() { Name = "Australia" , Capital = "Canberra" },
            new Country() { Name = "Bulgaria", Capital = "Sofia" },
            new Country() { Name = "Canada" , Capital = "Ottawa" },
        };
        this.SelectedCountries = new ObservableCollection<Country>()
        {
            this.Countries[0]
        };
    }
}

and in the xaml:
<telerik:RadAutoCompleteBox DataContext="{StaticResource ViewModel}"
                            ItemsSource="{Binding Countries}"
                            TextSearchPath="Name"
                            AutoCompleteMode="Suggest"
                            WatermarkContent="Type a Country"
                            SelectedItems="{Binding SelectedCountries, Mode=TwoWay}"
                            Width="400"/>

Hope this is helpful.

All the best,
Vladi
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
David
Top achievements
Rank 1
answered on 07 Apr 2013, 11:30 AM
Hi Vladi,

I want to search item by multiple properties.like "Name" and "Capital".
What is the syntax to write it in "TextSearchPath" or there is another way to do it?

David
0
Vladi
Telerik team
answered on 08 Apr 2013, 08:56 AM
Hello,

In order to achieve the desired behavior you will need to create a custom FilteringBehavior and set it to the AutoCompleteBox control.

You can check our runnable example project (TwoPropertiesFilteringBehavior) that demonstrates the necessary customization in our online SDK repository, you can download the entire SDK repository from here. Hope this is helpful.

Greetings,
Vladi
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
AutoCompleteBox
Asked by
John
Top achievements
Rank 1
Answers by
Vladi
Telerik team
David
Top achievements
Rank 1
Share this question
or