【问题标题】:c# UWP binding ComboBox SelectedItemc# UWP绑定ComboBox SelectedItem
【发布时间】:2017-04-30 06:09:55
【问题描述】:

您好,我似乎无法弄清楚为什么我的组合框保持空白:/

在页面加载时,第一个组合框会填充国家/地区(来自 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 反序列化(对问题不太重要):

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;
        }
    }
}

【问题讨论】:

  • 提示:复制粘贴代码时,取消缩进,使最外层有四个缩进空格。这样可以避免不必要的水平滚动。
  • 对不起,我下次会做的
  • 无需道歉。但你可以编辑这篇文章。

标签: c# data-binding combobox binding model-binding


【解决方案1】:

在 SelectedLand 属性设置器中,您需要为 SelectedLand 和 Gewesten 触发 PropertyChanged 事件。

它可能看起来像这样

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

如果您不为 Gewesten 触发 PropertyChanged 事件,那么该组合框将不知道要重新加载值。

【讨论】:

  • 完全做到了:) 谢谢先生
猜你喜欢
  • 2016-05-15
  • 1970-01-01
  • 2019-11-10
  • 2012-06-23
  • 2011-11-01
  • 2015-06-28
  • 2023-04-03
  • 2010-10-24
  • 1970-01-01
相关资源
最近更新 更多