我使用ComboBox
在XAML:組合框無法設置的DataContext
<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。
嗯......虛擬機中的一切都是靜態的。在XAML中執行綁定時,必須設置ItemsSource = {Binding ComponentCollection}(ObservableCollection),並在DataContext上需要類Component Data Source的實例。但既然它有靜態的方法,我不知道它會怎麼做的伎倆... – VasileF 2013-05-08 09:05:50
@Vasile MarianFălămaş,我應該做任何方法實例方法,而不是靜態? – 2013-05-08 09:54:48
標記最佳/正確答案是禮貌的。 – 2013-06-20 15:31:10