如何正确地绑定到windows phone 8.1上的combox ?我尝试了在winforms中通常要做的事情,但它不起作用。此外,这是一个设置页面是他们的任何标准做法,对于8.1手机商店应用程序创建设置页面的方式与silverlight相同。
在你问“是”之前,数据是他们的罚款证明了这一点。
public class City
{
public string id { get; set; }
public string timing_title { get; set; }
}
public class CitysList
{
public List<City> cityList { get; set; }
}我认为当从项目源设置时,DisplayMmember路径会起作用
<ComboBox x:Name="cboCitys" ItemsSource="{Binding}" DisplayMemberPath="{Binding timing_title}" HorizontalAlignment="Left" Margin="18,73,0,0" VerticalAlignment="Top" Width="343" Height="51">
</ComboBox> 我是如何处理数据的
popcornpk_Dal _dal = new popcornpk_Dal();
CitysList _mycities = await _dal.GetCityListAsync();
cboCitys.ItemsSource = _mycities.cityList;发布于 2015-08-24 15:38:18
DisplayMemberPath用于指定显示属性的路径,您不需要绑定它
DisplayMemberPath="timing_title"除此之外,如果您将combobox的itemSource绑定到一个集合属性,并在您的CitysList类中实现INotifyPropertyChanged,将会更加优雅,如下所示:
public class CitysList:INotifyPropertyChanged
{
private ObservableCollection<City> _citylist ;
public ObservableCollection<City> CityList
{
get
{
return _citylist;
}
set
{
if (_citylist == value)
{
return;
}
_citylist = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}Xaml
<ComboBox ItemsSource="{Binding CitysList}" DisplayMemberPath="timing_title" />不要忘记将DataContext设置为包含集合的类的实例,更新列表只需重新实例化它
CityList = new ObservableCollection<City>(await _dal.GetCityListAsync());更新
要设置dataContext,
First在代码背后创建一个CityList属性,
private CitysList _cityList ;
public CitysList CityList
{
get
{
return _cityList;
}
set
{
if (_cityList == value)
{
return;
}
_cityList = value;
OnPropertyChanged();
}
}对于第二个,使用以下命令将页面DataContext设置为代码后台
this.DataContext=this; //in the main constructor 或从Xaml使用
DataContext="{Binding RelativeSource={RelativeSource Self}}"Combobox将自动继承页面DataContext
将Third绑定到您的集合
<ComboBox x:Name="cboCitys" ItemsSource="{Binding CityList.CityList}" DisplayMemberPath="timing_title" HorizontalAlignment="Left" Margin="18,73,0,0" VerticalAlignment="Top" Width="343" Height="51">PS:你也可以考虑在你的代码中直接添加CityList集合,不需要添加一个类来保存这个集合!
https://stackoverflow.com/questions/32172420
复制相似问题