Let's say that you have an observable collection of object type Foo, and you have a custom ListView that the user will select from.
Your bound data object:
// property with getter / setter / INotifyPropertyChanged
ObservableCollection<Foo> MyCollection;
In XAML:
<ListView ItemsSource={Binding MyCollection} />
Is it more appropriate to bind to the 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 to create this and bind to the SelectedItem in XAML:
Foo SelectedObject { get; set; } // also raising property changed notifications
And why?
Both cases are acceptable, however which you choose usually depends on the design of your data models, and which method would require the least amount of code to get working.
A few rules I use to determine which one to select
If SelectedObject
cannot be null (such as an enum), and you need to default to be no item selected, then use SelectedIndex
.
If SelectedObject
may not be considered .Equals()
to any item in your Items
list, use SelectedIndex
. This is because SelectedItem
compares objects with the Items
collection using .Equals()
, so a reference comparism will return false which will result in your object not becomming selected.
This usually happens when your selected item comes from a different location than where your list of items is. For example, one database calls to load the Items
for the list, and a separate database call obtaining an object that includes a SelectedObject
property.
If you need to reference only one of SelectedObject
or SelectedIndex
in other parts of your code, use that one.
If your data models already have a SelectedIndex
or SelectedObject
property, then use that.
If all other things are equal, I use a SelectedObject
property to bind the SelectedItem
property.
This is because to me it makes more sense to refer to something like SelectedUser
instead of SelectedUserIndex
in the code behind, and I prefer to avoid looking up the item in the collection anytime I want to do something with the selected item.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With