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

Select First Item

2 Answers 32 Views
ListView
This is a migrated thread and some comments may be shown as answers.
George
Top achievements
Rank 1
George asked on 13 Sep 2017, 03:41 PM
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 Answers, 1 is accepted

Sort by
0
Lance | Manager Technical Support
Telerik team
answered on 13 Sep 2017, 06:27 PM
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
0
George
Top achievements
Rank 1
answered on 15 Sep 2017, 01:23 PM
Thanks, this worked great.
Tags
ListView
Asked by
George
Top achievements
Rank 1
Answers by
Lance | Manager Technical Support
Telerik team
George
Top achievements
Rank 1
Share this question
or