C# 如何将Web服务中的数据(ID)绑定到listview上的两个单选按钮(0和1),并期望结果为UWP中的json格式

C# 如何将Web服务中的数据(ID)绑定到listview上的两个单选按钮(0和1),并期望结果为UWP中的json格式,c#,windows,xaml,uwp,C#,Windows,Xaml,Uwp,//来自webservice的数据类,每个问题都有自己的ID public class Class1 { public int id { get; set; } public string question { get; set; } } //将生成以下json格式的预期结果的类 public class Rootobject { public strin

//来自webservice的数据类,每个问题都有自己的ID

     public class Class1
        {
            public int id { get; set; }
            public string question { get; set; }
        }
//将生成以下json格式的预期结果的类

     public class Rootobject
        {
            public string description { get; set; }
            public Booking booking { get; set; }
            public int rating { get; set; }
            public Questionnaire[] questionnaire { get; set; }
        }
//类中的“答案来自”复选框,我希望在选择每个文本框时获取问题的ID和答案的值0或1。我不想有任何问题没有答案

    public class Questionnaire
        {
            public int id { get; set; }
            public int answer { get; set; }
        }
//我的xaml代码,但我还没有将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 Foreground="Green" Name="ServiceYes" Content="Yes" GroupName="services" Checked="Exposure_Checked"></RadioButton>
                                    <RadioButton Foreground="Red" Name="ServiceNo" Content="No" GroupName="services" Checked="Exposure_Checked"></RadioButton>
                                </StackPanel>
                            </StackPanel>
                                </DataTemplate>
                        </ListView.ItemTemplate>

                    </ListView> 

要将int绑定到
IsChecked
属性
RadioButton
,我们应该能够
转换器
。我们可以通过实现接口和Convert方法来创建转换器

例如:

class IntConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        // Retrieve the format string and use it to format the value.
        int votenumber, currentone;
        if (parameter != null)
        {
            votenumber = System.Convert.ToInt32(value);
            currentone = System.Convert.ToInt32(parameter);

            if (votenumber == currentone)
                return true;
        }

        return false;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        int  currentone;
        if (parameter != null)
        {
            currentone = System.Convert.ToInt32(parameter);
            if (currentone==0)
            {
                return currentone;
            }
            else
            {
               return currentone;   
            }
        }
        return 0;
    }
}
public class Class1: INotifyPropertyChanged
{

    private int _id;
    private string _question;

    public int id
    {
        get { return _id; }
        set
        {
            _id = value;
            RaisePropertyChanged("id");
        }
    }

    public string question
    {
        get { return _question; }
        set
        {
            _question = value;
            RaisePropertyChanged("question");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string name)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }
}
要将同一id绑定到RadioButton的不同
IsChecked
属性,我们应该能够使用ValueConverter和ConverterParameter来实现它

XAML代码:

<ListView Margin="10,0,10,20" Background="White" x:Name="feedbackListView">
    <ListView.Resources>
        <local:IntConverter x:Key="IntToBoolConverter" />
    </ListView.Resources>
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical">
                <TextBlock TextWrapping="Wrap" Text="{Binding question}"></TextBlock>
                <StackPanel Orientation="Horizontal">
                    <RadioButton Foreground="Green" Name="ServiceYes" Content="Yes" GroupName="{Binding question}" Checked="Exposure_Checked" IsChecked="{Binding id,Mode=TwoWay,ConverterParameter=0,Converter={StaticResource IntToBoolConverter}}"></RadioButton>
                    <RadioButton Foreground="Red" Name="ServiceNo" Content="No" GroupName="{Binding question}" Checked="Exposure_Checked" IsChecked="{Binding id,Mode=TwoWay,ConverterParameter=1,Converter={StaticResource IntToBoolConverter}}"></RadioButton>
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

顺便说一下,我们应该能够设置一个组的不同
GroupName
。如果我们将相同的名称设置为所有
单选按钮的
组名
,则只能检查一个
单选按钮

感谢您的回复,但不够清晰。我已经在“反馈图像”上面附上了期望的用户界面图像,您可以查看该图像以获得进一步的澄清。我期待类似这样的结果作为“调查问卷”:[{“id”:1,“答案”:0},{“id”:2,“答案”:1},{“id”:3,“答案”:0},{“id”:4,“答案”:1}。两个单选按钮都必须具有公共ID,如果选择是或否,则值应不同IntConverter类如何与我自己的类一起工作request@OlayemiRondo我们可以将id绑定到
GroupName
,并将答案绑定到
IsChecked
。我们可以将
ConverterParameter
发送到
IntConverter
。当答案为0时,
IntConverter
将向
ServiceYes
返回true,向
ServiceNo
返回false。谢谢,如果我能同时获得xaml和后端sir的代码,我将不胜感激。别忘了,我正在使用列表视图,我将向json数组发送ID和应答(0或1)您的意思是用户可以选择RadioButton并将其保存到json数组吗?如果是这样,您应该能够为IsChecked使用双向绑定,并且需要实现ConvertBack方法。
<ListView Margin="10,0,10,20" Background="White" x:Name="feedbackListView">
    <ListView.Resources>
        <local:IntConverter x:Key="IntToBoolConverter" />
    </ListView.Resources>
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical">
                <TextBlock TextWrapping="Wrap" Text="{Binding question}"></TextBlock>
                <StackPanel Orientation="Horizontal">
                    <RadioButton Foreground="Green" Name="ServiceYes" Content="Yes" GroupName="{Binding question}" Checked="Exposure_Checked" IsChecked="{Binding id,Mode=TwoWay,ConverterParameter=0,Converter={StaticResource IntToBoolConverter}}"></RadioButton>
                    <RadioButton Foreground="Red" Name="ServiceNo" Content="No" GroupName="{Binding question}" Checked="Exposure_Checked" IsChecked="{Binding id,Mode=TwoWay,ConverterParameter=1,Converter={StaticResource IntToBoolConverter}}"></RadioButton>
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
public class Class1: INotifyPropertyChanged
{

    private int _id;
    private string _question;

    public int id
    {
        get { return _id; }
        set
        {
            _id = value;
            RaisePropertyChanged("id");
        }
    }

    public string question
    {
        get { return _question; }
        set
        {
            _question = value;
            RaisePropertyChanged("question");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string name)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }
}