//这是针对列表中的两个单选按钮,当我改变主意单击选项是而不是否时,它会向列表中添加另一个值,而不是将列表中的答案值从 0 t0 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;
}
我的 Xalm 代码,它包含列表视图和我的单选按钮,我将 Id 绑定到两个按钮
<ListView Margin="10,0,10,20" Background="White" x:Name="feedbackListView">
<ListView.ItemTemplate>
<DataTemplate x:DataType="data:Rootobject">
<StackPanel Orientation="Vertical">
<TextBlock TextWrapping="Wrap" Text="{Binding question}"></TextBlock>
<StackPanel Orientation="Horizontal">
<RadioButton DataContext="{Binding id}" Foreground="Green" Name="ServiceYes" Content="Yes" Tag="Yes" Checked="Service_Checked" ></RadioButton>
<RadioButton DataContext="{Binding id}" Foreground="Red" Name="ServiceNo" Content="No" Tag="No" Checked="Service_Checked"></RadioButton>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我的模型类
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;
}
}