kikis
kikis

Reputation: 103

Get Entry value in ListView Xamarin forms

I am novice in Xamarin Forms. I am trying to catch all Entry value in ListView when I click on the Label "OK".

Here is my ListView XAML code:

<StackLayout>
    <ListView x:Name="TestList">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout Padding="10,5,5,5" VerticalOptions="StartAndExpand" Orientation="Vertical">
<!-- here I am getting the value of word-->
                        <Label TextColor="Black" Text="{Binding  Word1}"  FontSize="15" />
<!-- here I am putting some text that I want to catch after-->
                        <Entry Placeholder="Your traduction" x:Name="mytestEntry" Completed="Entry_Completed" PlaceholderColor="#bababa" FontSize="16" />
                        <Label TextColor="Black" Text="{Binding  ValueAnswer}" FontSize="15" />
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
<!-- when I tapp on the label "OK", I would like to catch all entry value in ListView -->
    <Label Text="OK"  FontSize="30" HorizontalOptions="Center" TextColor="#A4978E">
        <Label.GestureRecognizers>
            <TapGestureRecognizer Tapped="OnResultTest" />
        </Label.GestureRecognizers>
    </Label>
</StackLayout>

Here is my C# code :

List<MyWords> myWords = mywordsdatabase.GetListWords();
List<TestModel> ListWordsTest = new List<TestModel>();
foreach (MyWords w in myWords)
{
    var testmodel = new TestModel
    {
        Word1 = w.Word1.ToString(),
        Entryreponse = "put some text here",
        ValueAnswer = ""
    };

    ListWordsTest.Add(testmodel);

    i++;
}

TestList.ItemsSource = ListWordsTest;

I have tried this method to catch entry but it is not working, I would like to catch all the listview entry when I tapped on "OK" Label and change their values to do other stuff :

public void OnResultTest(object sender, EventArgs args)
{
    foreach (var item in TestList.ItemsSource)
    {
        // cast the item 
        var dataItem = (TestModel)item;
        Console.WriteLine( dataItem.Entryreponse); //output : "put some text here"
    }
}

If you have a better solution I will take, thanks in advance.

Upvotes: 0

Views: 2173

Answers (1)

Jason
Jason

Reputation: 89092

first, you need to bind the value of Entry to a property in your model

<Entry Placeholder="Your traduction" Text="{Binding Entryreponse}" ... />

then in your code behind

foreach (var item in ListWordsTest)
{
   Console.WriteLine(item.Entryreponse);
}

Upvotes: 1

Related Questions