简体   繁体   中英

C# WPF binding json data with checkbox into a listbox

I have a project in C# with Windows Presentation Foundation (WPF). My json is like this

[
  {
    "question": "SelOneRad",
    "text": "This is a Select One - Radio question.",
    "type": "Select (Radio Button)",
    "rows": [
      {
        "text": "Option 1",
        "checked": false,
        "fields": "1",
        "column": "SelOneRad"
      },
      {
        "text": "Option 2",
        "checked": false,
        "fields": "2",
        "column": "SelOneRad"
      },
      {
        "text": "Option 3",
        "checked": false,
        "fields": "3",
        "column": "SelOneRad"
      },
      {
        "text": "Others, please specify: ",
        "checked": false,
        "fields": "4",
        "column": "SelOneRad"
      },
      {
        "text": "None ",
        "checked": false,
        "fields": "5",
        "column": "SelOneRad"
      }
    ]
  }
]

For the rows: i need to have a checkbox and the name from the field text.

I'm new in WPF.

Thanks in advance.

Not sure if this is what you're looking for, if it's wrong could you expand on your explanation.

<ListView ItemsSource="{Binding rows}"> //the JSON array you want to bind to
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBlock Text={Binding text}/> //JSON array's fields here (rows.text)
            <CheckBox IsChecked="{Binding checked}/> //JSON array's fields here (rows.checked)
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView> 

If you want to create a class based off your JSON you can go to Edit > Paste Special > Paste JSON as Classes to do this you must be in a .cs file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM