kikis
kikis

Reputation: 103

Change listview label value

I am a novice in Xamarin forms.

I would like to change my listview label value when I click on the Label "OK"

Here is my Xaml:

            <ListView x:Name="TestList" >
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell >                           
                            <StackLayout >
                                <Label TextColor="Black" Text="{Binding  ValueAnswer}"  />
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

            <Label Text="OK" >
                <Label.GestureRecognizers>
                    <TapGestureRecognizer Tapped="OnResultTest"  />
                </Label.GestureRecognizers>
            </Label>
        </StackLayout>

Here is my code:

List<MyWords> myWords = mywordsdatabase.GetListAnswer();
 List<TestModel> ListWordsTest = new List<TestModel>();
            foreach (MyWords w in myWords)
            {

                var testmodel = new TestModel
                {

                    ValueAnswer = "A"

                };

                ListWordsTest.Add(testmodel);

            }

            TestList.ItemsSource = ListWordsTest;

I would like to change my label value to "B" by selecting item, When I click on "Ok" Label.

I cannot see How to make it.

Here my little step for the "OK" Label :

public void OnResultTest(object sender, EventArgs args) {         

        }

If you have a better solution. I will take

Upvotes: 1

Views: 98

Answers (1)

Brandon
Brandon

Reputation: 3266

You can grab the label that was clicked from the sender that's passed into OnResultTest. Once you get the sender, then you can grab your class via DataContext.

public void OnResultTest(object sender, EventArgs args) {         
    var items =TestList.ItemsSource as List<TestModel>;
    if (items!= null) 
    {
        foreach (var item in items)
        {
            item.ValueAnswer = "B";
        }
    }
}

Upvotes: 2

Related Questions