I'm working with Xamarin.Forms in a PCL project.
I have a page/screen where there are an ListView control. I have created a custom DataTemplate for ViewCell.
This ViewCell has different controls: some Labels, one Button and also a Entry.
<ListView x:Name="lvProducts" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout BackgroundColor="#FFFFFF" Orientation="Vertical">
<Grid Padding="5">
...
<Button Grid.Row="0" Grid.Column="1" Text="X"
CommandParameter="{Binding MarkReference}"
Clicked="DeleteItemClicked" />
...
<StackLayout Grid.Row="2" Grid.ColumnSpan="2" Orientation="Horizontal" >
<Label Text="Ref.: " FontSize="24" FontAttributes="Bold" TextColor="#000000" />
<Label Text="{Binding Reference}" FontSize="24" TextColor="#000000" />
</StackLayout>
...
<Entry Grid.Row="3" Grid.Column="1" Text="{Binding NumElements}"
Keyboard="Numeric" Placeholder="" FontSize="24"
HorizontalTextAlignment="Center" Focused="OnItemFocus"
Unfocused="OnItemUnfocus" />
</Grid>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
I want to achieve two things with this Entry control that I'm not able achieve:
First, when I add a new item, I would like that this new item has his Entry the focus, ready to start typing.
Second, when the user ends to write a value into the Entry, I would like to change the value of the behind. I would like know which Entry of ListView has modified. I tried to use the Unfocused event, but in the params of the method that launches only has a sender param that returns the Entry object, no reference about the model that has binded.
public void OnItemUnfocus(object sender, EventArgs e)
{
Entry entry = (Entry)sender;
//Here I would like to know the model object that's binded
//with this Entry / CellView item
}
How I can achieve these two points?
OnBindingContextChanged
in the cell, and then obtain the object withvar object = (YourClass)BindingContext
. This way you can access the object in the data source whenever you need it. – papafe