簡體   English   中英

VB.NET WPF - 使用 Z0ECD11C1D7A287401D148A23BBD7 數組中的值填充 Combobox

[英]VB.NET WPF - Populate Combobox with values from a JSON array

我在本地文件(local.json)中有一個 json 數組,如下所示

[
  {
    "Name": "Element1",
    "Url": "https://someurlforelement1.com"
  },
  {
    "Name": "Element1",
    "Url": "https://someurlforelement2.com"
  }
]

vb中對應的class

Public Class MyElement
    Public Property Name As String
    Public Property Url As String
End Class

我正在嘗試在應用程序開始時讀取 json 文件,並使用 MyElement 對象列表填充 combobox。 目標是顯示 Name 屬性,但使用對象的 Url 屬性作為值。 谷歌搜索后我嘗試了幾件事,但我對 VB.NET 還很陌生,不得不接手其他開發人員的工作,所以需要幫助:) 謝謝

編輯:這是從下面提供的答案開始的最終代碼:

Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
    ' Read json and setup the combobox
    Dim rawjson = File.ReadAllText(My.Application.Info.DirectoryPath & "\local.json")
    Dim lst = JsonConvert.DeserializeObject(Of List(Of MyElement))(rawjson)
    For Each i In lst
        filterComboBox.Items.Add(i)
    Next
    filterComboBox.DisplayMemberPath = "Name"
    filterComboBox.SelectedValuePath = "Url"

通過使用它,您可以獲得 class 對象的列表,但為此您需要導入 Newtonsoft.Json

 Dim rawjson = File.ReadAllText(My.Application.Info.DirectoryPath & "\local.json")

            Dim lst = JsonConvert.DeserializeObject(Of List(Of MyElement))(rawjson)

現在使用此列表創建循環並填寫您的 combobox

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM