我ComboBox
在 XAML 中使用:
<ComboBox x:Name="Combobox1" ItemsSource="{Binding}" Margin="0,0,300,0"
Width="100" FontSize="30" />
在后面的代码中,我将其值设置为:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
Combobox1.DataContext = ComponentDataSource.ComponentCollection;
}
现在我有一个数据源:
public class ComponentDataSource
{
private static ObservableCollection<ComponentGroup> _componentcollection;
public static ObservableCollection<ComponentGroup> ComponentCollection
{
get { return _componentcollection; }
}
public static async void CheckJson(object sender, object e)
{
var client = new HttpClient();
client.MaxResponseContentBufferSize = 1024 * 1024;
try
{
var response = await client.GetAsync(new Uri("URI"));
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadAsStringAsync();
var jobj = JObject.Parse(result);
var list = jobj.Children()
.Cast<JProperty>()
.Select(p => new ComponentGroup()
{
Name = p.Name,
Type = (string)p.Value["P1"],
Value = (string)p.Value["P2"]
})
.ToList();
_componentcollection = new ObservableCollection<ComponentGroup>(list);
}
catch (HttpRequestException ex)
{
}
}
}
由于某种原因,这些项目没有出现在ComboBox
. 我得到的只是一个空的ComboBox
.
有人可以帮我吗?
编辑1:嗨,我知道我错过了一些简单的东西,但如果有人可以帮助我,我将不胜感激。顺便说一句,如果您想要代码,请告诉我,我会将其上传到 skydrive。