I have a ListView that includes a TimePicker:
<ListView.ItemTemplate> <DataTemplate> <ViewCell> <StackLayout Orientation="Horizontal"> <TimePicker Time="{Binding MyTimeSpan}" PropertyChanged="OnTimePickerPropertyChanged" > <TimePicker.Format>hh:mm tt</TimePicker.Format> </TimePicker> <Label Text="{Binding Text}"> <Label.GestureRecognizers> <TapGestureRecognizer Tapped="ItemTextTapped" CommandParameter="{Binding Id}"></TapGestureRecognizer> </Label.GestureRecognizers> </Label> </StackLayout> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView>
Then the event handler:
void OnTimePickerPropertyChanged(object sender, PropertyChangedEventArgs args) { if (!args.PropertyName.Equals("Time")) return; TimePicker picker = (TimePicker)sender; // which Item is it? }
How can I tell which Item is associated with the TimePicker?
FYI, Firefox does not work with your website.
@GerryH Get the index using the code below:
void OnTimePickerPropertyChanged(object sender, PropertyChangedEventArgs args) { if (!args.PropertyName.Equals("Time")) return; TimePicker picker = (TimePicker)sender; // which Item is it? var singleModel = picker.BindingContext as YourModel; var index = ItemsSoruce.IndexOf(singleModel); }
ItemsSoruce
here is your list view's ObservableCollection items source.
Answers
Disable your ad-block plug-in. Same thing happened to me.
Huh? What you trying to accomplish by getting the UI element itself? That raises red flags.
You already have the time that was chosen by the user because you're binding to it.
And for some weird reason you're also raising a CLR event when the property is changed: That's going to include the
sender
which is the picker.And now you also want to get the UI picker element when you tap on a label?
That reads a lot like you're struggling to find a way to accomplish something, because that's a metric ton of redundancy and logic incorrectly being handled at the UI layer.
So what is it you're really trying to acoomplish and we can try to get you working along a better path.
I am using an ObservableCollection. It has multiple items in it. The user changes the time for one of the items in the collection, but I have no way of know which one.
What? What do you mean "I don't know which one"? Huh?
The user taps the
Picker
in theListView
. The time value is binded to the property on that item - on the model itself.The model that gets the change knows the change has happened. And since its all binded your UI updates automatically.
Are you trying to micro-manage or react to a change on element of the collection - from some outside class?
AGAIN I'm asking: What is the goal? What is it you're trying to achieve? Not how are you trying to achieve it - because what you're doing is weird... So not the how but the why. Why are you trying to track which element of the collection got a new time?
Goal - I'd like to update the sqlite database when the user updates the time field. But I don't which record to update.
@GerryH Get the index using the code below:
ItemsSoruce
here is your list view's ObservableCollection items source.When you got the record you got the
ID
from the database, right? So when you update the record, you use that sameID
. You shouldn't need the index from the ListView. PIcture it this way... In the future you may want to let the user sort or filter the list you got from the database: Fetch once, then sort or filter. That would make the index an invalid way to track it back to the database. But itsID
will always be itsID
.LandLu, my model load from sqlite, and it includes the Id (index). Thanks!