How to bind to SelectedItems

4 posts, 0 answers
  1. John
    John avatar
    1 posts
    Member since:
    Aug 2010

    Posted 01 Nov 2012 Link to this post

    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
  2. Vladi
    Admin
    Vladi avatar
    744 posts

    Posted 02 Nov 2012 Link to this post

    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.

  3. UI for WPF is Visual Studio 2017 Ready
  4. David
    David avatar
    4 posts
    Member since:
    Oct 2012

    Posted 07 Apr 2013 Link to this post

    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
  5. Vladi
    Admin
    Vladi avatar
    744 posts

    Posted 08 Apr 2013 Link to this post

    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.

Back to Top