ListView获取多个条目值以验证和发送[关闭]

问题描述 投票:0回答:1

我希望你能帮助我。

我有一个来自API的调查,我想在ListView中显示它们,但我的问题是,如何通过单击发送按钮来验证每个条目?

我的CodeBehind

public class SurveyList
{
    public List<QuestionList> Questions { get; set; }
}

public class QuestionList
{
    public string QuestionText { get; set; }
    public string QuestionCode { get; set; }
}

public partial class SurveyPage : ContentPage
{

    public SurveyPage()
    {
        InitializeComponent();

        var surveyListData = new List<QuestionList>
        {
            new QuestionList { QuestionText = "Question 1?", QuestionCode = "01" },
            new QuestionList { QuestionText = "Question 2?", QuestionCode = "02" }
        };

        surveyList.ItemsSource = surveyListData;
    }

    void Handle_Clicked(object sender, System.EventArgs e)
    {
        // Button Clicked Get Values Here and Do Something
    }
}

}

我的XAML

<ListView x:Name="surveyList"
          HasUnevenRows="true"
          SeparatorVisibility="Default" 
          BackgroundColor="White">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout Padding="10" BackgroundColor="Purple">
                    <StackLayout Spacing="10" VerticalOptions="Start" HorizontalOptions="FillAndExpand" BackgroundColor="Olive">
                        <Label Text="{Binding QuestionLabel}" TextColor="Navy"/>
                        <Picker x:Name="QuestionPicker">
                            <Picker.Items>
                                <x:String>Yes</x:String>
                                <x:String>No</x:String>
                            </Picker.Items>
                        </Picker>
                    </StackLayout>
                    <StackLayout Spacing="20" VerticalOptions="End" HorizontalOptions="FillAndExpand" BackgroundColor="Maroon">
                        <Button x:Name="surveyButton"
                                Text="Enviar"
                                TextColor="White"
                                BackgroundColor="{StaticResource dpGreen}"
                                Clicked="Handle_Clicked"/>
                    </StackLayout>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

使用此代码,我将测试如何从单击的按钮获取所有值

c# xamarin xamarin.forms
1个回答
1
投票

使用Foreach作为@Andrew说,解决问题。

void Handle_Clicked(object sender, System.EventArgs e)
    {
        foreach(var getValues in survey)
        {
            getValuesOfPickers.Text = getValues.QuestionCode;
        }
    }

不确定它是否完全解决了,但正如@Andrew所说,原来的问题得到了回答

© www.soinside.com 2019 - 2024. All rights reserved.