C# 当我在UWP中将选项从“是”更改为“否”时,如何从列表中替换选中单选按钮的值,反之亦然

C# 当我在UWP中将选项从“是”更改为“否”时,如何从列表中替换选中单选按钮的值,反之亦然,c#,windows,xaml,mobile,uwp,C#,Windows,Xaml,Mobile,Uwp,//这适用于列表上的两个单选按钮,当我改变主意单击选项“是”而不是“否”时,它会向列表中添加另一个值,而不是将列表中答案的值从0更改为1或从1更改为0 private void Service_Checked(object sender, RoutedEventArgs e) { RadioButton rb = sender as RadioButton; if (rb != null) {

//这适用于列表上的两个单选按钮,当我改变主意单击选项“是”而不是“否”时,它会向列表中添加另一个值,而不是将列表中答案的值从0更改为1或从1更改为0

private void Service_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton rb = sender as RadioButton;
            if (rb != null)
            {
                string answer = rb.Content.ToString();
                switch (answer)
                {
                    case "Yes":
                        value = 1;
                        break;
                    case "No":
                        value = 0;
                        break;
                }
            }
            int RID = int.Parse(rb.DataContext.ToString());
            Questionnaire you = new Questionnaire();
            you.id = RID;
            you.answer = value;
            selectedItems.Add(you);
        }
//选择提交按钮时

private void btnSend_Click(object sender, RoutedEventArgs e)
        {
            refno.id = txtRefNo.Text;
            string comment = txtComment.Text;

            Rootobjectsssss ratingClass = new Rootobjectsssss()
            {
                description = comment,
                booking = refno,
                rating = rating,
                questionnaire = selectedItems.ToArray(),
            };

            json = string.Empty;
            json = JsonConvert.SerializeObject(ratingClass, Formatting.Indented);
            Debug.WriteLine(json);
        }
//我在哪里得到绑定到两个单选按钮的ID

async void getData()
        {
            string url = "http://62.173.41.5:7500/NNRAService/webresources/customerResources/getQuestionaire";
            HttpClient client = new HttpClient();

            HttpResponseMessage response = await client.GetAsync(new Uri(url));
            var jsonString = await response.Content.ReadAsStringAsync();

            JsonArray root = JsonValue.Parse(jsonString).GetArray();
            for (uint i = 0; i < root.Count; i++)
            {
                string Question = root.GetObjectAt(i).GetNamedString("question");
                string Id = root.GetObjectAt(i).GetNamedNumber("id").ToString();
                var chan = new Class1
                {
                    question = Question,
                    id = Id,

                };
                feedbackList.Add(chan);
            };
            feedbackListView.ItemsSource = feedbackList;

        }

我相信您总是将changedItem添加到SelectedItem列表中,这就是为什么它添加而不是替换

在将项目添加到selectedItems之前,您可能需要进行检查。如下所示:

private void Service_Checked(object sender, RoutedEventArgs e)
    {
        RadioButton rb = sender as RadioButton;
        if (rb != null)
        {
            string answer = rb.Content.ToString();
            switch (answer)
            {
                case "Yes":
                    value = 1;
                    break;
                case "No":
                    value = 0;
                    break;
            }
        }
        int RID = int.Parse(rb.DataContext.ToString());

        Questionnaire temp = selectedItems.FirstOrDefault(x => x.id == RID);
        if(temp == null)
        {
           Questionnaire you = new Questionnaire();
           you.id = RID;
           you.answer = value;
           selectedItems.Add(you);
        }
        else
        {
           temp.answer = value;
        }
    }

谢谢,很好用。如何验证用户在提交之前必须选择是或否。我的意思是所有问题都必须回答。您可以随时验证selectedItems列表中的计数和您的问题总数,两个计数应该匹配。然而,我觉得一定有更好的方法,在不了解解决方案设计的情况下回答这个问题是不可行的。请针对您的具体问题打开另一个问题,并为新问题添加更多详细信息。
public class Class1
    {
        public string id { get; set; }
        public string question { get; set; }
    }


    public class Rootobjectsssss
    {
        public string description { get; set; }
        public Boooking booking { get; set; }
        public int rating { get; set; }
        public Questionnaire[] questionnaire { get; set; }
    }

    public class Boooking
    {
        public string id { get; set; }
    }

    public class Questionnaire
    {
        public int id { get; set; }
        public int answer { get; set; }
    }
private void Service_Checked(object sender, RoutedEventArgs e)
    {
        RadioButton rb = sender as RadioButton;
        if (rb != null)
        {
            string answer = rb.Content.ToString();
            switch (answer)
            {
                case "Yes":
                    value = 1;
                    break;
                case "No":
                    value = 0;
                    break;
            }
        }
        int RID = int.Parse(rb.DataContext.ToString());

        Questionnaire temp = selectedItems.FirstOrDefault(x => x.id == RID);
        if(temp == null)
        {
           Questionnaire you = new Questionnaire();
           you.id = RID;
           you.answer = value;
           selectedItems.Add(you);
        }
        else
        {
           temp.answer = value;
        }
    }