Binding SelectedItem vs SelectedIndex - When do I need to select one by one?

Say you have an observable collection of the type of the Foo object, and you have a custom ListView that the user selects.

Your linked data object:

// property with getter / setter / INotifyPropertyChanged ObservableCollection<Foo> MyCollection; 

In XAML:

 <ListView ItemsSource={Binding MyCollection} /> 

Is it more appropriate to link to SelectedIndex in XAML and create the following in your data object:

 int SelectedIndex { get; set; } // also raising property changed notifications Foo SelectedObject { get { return MyCollection[SelectedIndex]; } } 

Or create this and bind to SelectedItem in XAML:

 Foo SelectedObject { get; set; } // also raising property changed notifications 

And why?

+5
source share
4 answers

Both cases are acceptable, but what you choose usually depends on the design of your data models, and which method will require the least amount of code to work.

A few rules that I use to determine which one to choose

  • If SelectedObject cannot be null (for example, an enumeration), and you want the selected item not to be the default, use SelectedIndex .

  • If SelectedObject cannot be considered .Equals() for any item in your Items list, use SelectedIndex . This is because SelectedItem compares objects with the Items collection using .Equals() , so the reference comparative example returns false, which will result in your object not being selected.

    This usually happens when the selected item comes from a place other than your list of items. For example, one database causes the Items to be loaded for a list and a separate database call that receives an object that includes the SelectedObject property.

  • If you need to reference only one of SelectedObject or SelectedIndex in other parts of your code, use it.

  • If your data models already have the SelectedIndex or SelectedObject property, then use this.

  • If all other things are equal, I use the SelectedObject property to bind the SelectedItem property.

    This is because it makes sense for me to refer to something like SelectedUser instead of SelectedUserIndex in the code behind, and I prefer to avoid searching for an item in the collection anytime I want to do something with the selected item.

+2
source

Depending on your needs. If you need to install the selected item, you will need the second version. If you need a dedicated index, you need the first version. If you do not need it, it depends on your personal preferences.

0
source

Both approaches are wonderful. Use the one you think is easier to use. I do not often rely on indexes, but your business may be different.

If you look at the MVVM libraries, Caliburn.Micro, for example, expects you to use the SelectedObject approach. He clearly supports this agreement.

And pay attention to the properties of the collection. In general, it’s better to have read-only automatic properties and set them in the constructor (notification of a change in properties is not required). It is not recommended to change instances of collections. While WPF supports it, it is much easier to obey the collection events in your own code if the instance does not change.

0
source

I find binding to SelectedObject is much cleaner if you don't need access to SelectedIndex

The only time I use SelectedIndex is when I want to set the default value to 0 to select the first item.

0
source

Source: https://habr.com/ru/post/1203305/


All Articles