2016-12-14 84 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

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