2016-12-14 159 views
0

你好,我似乎無法弄清楚,爲什麼我的COMBOX保持爲空:/C#UWP綁定組合框的SelectedItem

在頁面加載第一COMBOX獲取與國家(從JSON)填充,第二個組合框應填充時,國家在第一個組合框中被選中。我試圖在屬性中獲取SelectedItem(country)作爲字符串... SelectedItem的類型是ComboBoxItem?我認爲這是錯誤的地方。

的(視圖)模型,其中所述排序綁定屬性是:

public class LocalityModel : NotifyProp 
{ 
    #region properties 
    private static List<LocalityJSON> dataList; 
    public List<LocalityJSON> DataList 
    { 
     get 
     { 
       return dataList; 
     } 
     set { 
      dataList = value; 
      RaisePropertyChanged("Landen"); 
      RaisePropertyChanged("Gewesten"); 
     } 
    } 

    public List<string> Landen 
    { 
     get { if (DataList == null) return null; return (from s in DataList orderby s.Land select s.Land).Distinct().ToList<string>(); } 
    } 
    public string SelectedLand { get; set; } 
    public List<string> Gewesten { 
     get { if (DataList == null) return null; return (from s in DataList where s.Land.Equals(SelectedLand) select s.Gewest).Distinct().ToList<string>(); } 
    } 
    #endregion 
    #region ctor 
    public LocalityModel() 
    { 
     FillDataList(); 
    } 
    #endregion 
    #region methodes 
    public async void FillDataList() 
    { 
     if (DataList == null) 
     { 
      DataList = await EVNT.Entries(); 
     } 
    } 
    #endregion 
} 

的MainPage XAML(綁定):

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" DataContext="{Binding Source={StaticResource LocalityModel}}"> 
... 
     <TextBlock x:Name="txbCountry" Style="{StaticResource InfoLabelCountry}" /> 
     <ComboBox x:Name="cboCountry" Style="{StaticResource CountryBox}" ItemsSource="{Binding Landen}" SelectedItem="{Binding SelectedLand, Mode=TwoWay}" /> 
     <TextBlock x:Name="txbGewest" Style="{StaticResource InfoLabelGewest}" /> 
     <ComboBox x:Name="cboGewest" Style="{StaticResource GewestBox}" ItemsSource="{Binding Gewesten}" /> 

INotifyPropertyChanged的:

public class NotifyProp : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

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

爲模型JSON:

public class LocalityJSON 
{ 
    public string FB_ID { get; set; } 
    public string Land { get; set; } 
    public string Gewest { get; set; } 
    public string City { get; set; } 
} 

的JSON deserialisation(對這個問題不太重要):

public class EVNT 
{ 
    public async static Task<List<LocalityJSON>> Entries() 
    { 
     using (var client = new HttpClient()) 
     { 
      client.BaseAddress = new Uri(@"http://e-vnt.com/admin/core/api/"); 
      HttpResponseMessage response = await client.GetAsync("localityApi"); 
      if (response.IsSuccessStatusCode) 
      { 
       String s = await response.Content.ReadAsStringAsync(); 
       List<LocalityJSON> entries = JsonConvert.DeserializeObject<List<LocalityJSON>>(s); 
       return entries; 
      } 
      else 
       return null; 
     } 
    } 
} 

enter image description here

+0

提示:在複製粘貼代碼時,取消縮進以使最外層的hs縮進四個空格。這可以避免不必要的水平滾動。 –

+0

我的歉意,我會在下次 –

+0

沒有必要道歉。但你可以編輯這篇文章。 –

回答

2

在你SelectedLand屬性setter你需要火PropertyChanged事件的兩個SelectedLand和Gewesten。

它可能會是這個樣子

private string _SelectedLand; 
public string SelectedLand 
{ 
    get 
    { 
     return _SelectedLand; 
    } 
    set 
    { 
     _SelectedLand = value; 
     RaisePropertyChanged("SelectedLand"); 
     RaisePropertyChanged("Gewesten"); 
    } 
} 

,如果你不火PropertyChanged事件的Gewesten那麼組合框將不知道重裝的值。

+0

完全做到了:)謝謝先生 –

相關問題