Reputation: 992
I have a listview with an ItemsSource of a list of strings, and the DataTemplate is an entry cell text is bound to the string. If I click on the entry cell and start typing I can see my changes being made as I type, but the as soon as I click on an entry element outside of the ListView my changes are lost.
<StackLayout Orientation="Horizontal">
<Label>Number of Players</Label>
<Entry Text="3" />
</StackLayout>
<ListView Header="Players" ItemsSource="{Binding Players}">
<ListView.ItemTemplate>
<DataTemplate>
<EntryCell Text="{Binding .}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
In the viewmodel I have the following code in the constructor which adds the initial elements:
Players = new List<string>();
Players.Add("test1");
Players.Add("test2");
I also define the property for Players in the viewmodel
public List<string> Players { get; }
The only time it happens is the first time I click on the entry box next to the number of players label. If I click on empty space I can see the blue border of the selected listview item disappear but the value remains the value I changed it to, but as soon as I click on the entry the values for the entry cells in the list view revert to their original values.
I feel like I've encountered something similar in WPF, which I just responded to by adding UpdateSourceTrigger=PropertyChanged to my binding. As far as I know that isn't a thing in Xamarin.Forms (at least not yet).
Any ideas how I can keep the first value entered into the listview?
The file I'm working on is in a Xamarin.Forms Portable class library, and I'm testing it using the Universal Windows(UWP) build.
Upvotes: 0
Views: 772
Reputation: 992
Well, I haven't figured out the problem with strings yet, but I did work around the issue. My next step was going to be to encapsulate the Player as an object instead of a string, so I moved forward with this and lo-and-behold the binding worked the way it should without the reset.
I added the Player class, for now the player only has a name:
public class Player
{
public string Name { get; set; }
}
All that changed about the View was that I now referenced the Name property of the Player object rather than the string of a list of strings:
<ListView Header="Players" ItemsSource="{Binding Players}">
<ListView.ItemTemplate>
<DataTemplate>
<EntryCell Text="{Binding Name}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Can anyone tell me why the List of strings was acting funny? It doesn't seem obvious to me at all if it is a feature of Xamarin.Forms. Smells like a bug, to be honest.
Upvotes: 0