Select First Item

3 posts, 0 answers
  1. George
    George avatar
    8 posts
    Member since:
    Oct 2012

    Posted 13 Sep Link to this post

    It seems RadListView does not have an Items property nor does it have a SelectedIndex.  What's the best way to select the first item in the listview after the data has been  bound?
  2. Lance | Tech Support Engineer, Sr.
    Admin
    Lance | Tech Support Engineer, Sr. avatar
    419 posts

    Posted 13 Sep Link to this post

    Hi George,

    For this you can use the SelectedItem property, you can set it using the first item in the bound collection.

    Here is an example of binding it to a "SelectedItem" in your view model:

    View

    <data:RadListView ItemsSource="{Binding Emails}" SelectedItem="{Binding SelectedMail, Mode=TwoWay}"/>

    ViewModel

    public class ViewModel : INotifyPropertyChanged
        {
            private Mail selectedMail;
     
            public ViewModel()
            {
                this.Emails = new ObservableCollection<Mail> {
                    new Mail{ Sender = "Terry Tye",  Subject = "Re: Summer Vacation"},
                    new Mail{ Sender = "Felicia Keegan",  Subject = "Seminar Invitation"},
                    new Mail{ Sender = "Jared Linton",  Subject = "Discount code"},
                    new Mail{ Sender = "Mark Therese",  Subject = "Quick feedback"},
                    new Mail{ Sender = "Elvina Randall",  Subject = "Happy Birthday!"},
                    new Mail{ Sender = "Emilia Porter",  Subject = "Check the attachment"},
                    new Mail{ Sender = "Jared Linton",  Subject = "Gillian Flynn"},
                    new Mail{ Sender = "Felicia Keegan",  Subject = "Re: Summer Vacation"},
                    new Mail{ Sender = "Felicia Keegan",  Subject = "Pictures"},
                };
     
                SelectedMail = Emails.FirstOrDefault();
            }
     
            public ObservableCollection<Mail> Emails { get; set; }
     
            public Mail SelectedMail
            {
                get { return selectedMail; }
                set { selectedMail = value; OnPropertyChanged();}
            }
     
            public event PropertyChangedEventHandler PropertyChanged;
             
            protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }


    Notice that setting the SelectedEmail is what notifies the RadListView which item is to be selected because of the Two-Way binding.

    Regards,
    Lance | Tech Support Engineer, Sr.
    Progress Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. George
    George avatar
    8 posts
    Member since:
    Oct 2012

    Posted 15 Sep in reply to Lance | Tech Support Engineer, Sr. Link to this post

    Thanks, this worked great.
Back to Top