【问题标题】:Combo Box Binding Phone 8.1 UAP组合框绑定电话 8.1 UAP
【发布时间】:2015-08-23 23:08:51
【问题描述】:

如何正确绑定到 windows phone 8.1 上的组合框我尝试了我通常在 winforms 中执行的操作,但没有成功。这也是设置页面的标准做法,8.1 Phone Store 应用程序可以像 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>  

我如何对数据进行 Fetech

popcornpk_Dal _dal = new popcornpk_Dal();

CitysList _mycities = await _dal.GetCityListAsync();
cboCitys.ItemsSource = _mycities.cityList;

【问题讨论】:

    标签: c# xaml windows-phone-8.1


    【解决方案1】:

    DisplayMemberPath用于指定显示属性的路径,不需要绑定

    DisplayMemberPath="timing_title"
    

    此外,如果您将comboboxitemSource 绑定到 Collection 属性,并在您的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

    首先在代码隐藏中创建CityList属性,

     private CitysList _cityList ;
        public CitysList CityList
        {
            get
            {
                return _cityList;
            }
    
            set
            {
                if (_cityList == value)
                {
                    return;
                }
    
                _cityList = value;
                OnPropertyChanged();
            }
        }
    

    第二,使用

    将页面DataContext设置为codebehind
    this.DataContext=this; //in the main constructor 
    

    或使用 Xaml

    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    

    Combobox会自动继承页面DataContext

    第三个绑定到您的收藏

    <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 集合,无需添加类来保存该集合!

    【讨论】:

    • 我一直使用项目源,你能告诉我如何为其设置数据上下文吗?谢谢,我把它标记为答案?
    • 我在 CityList 上收到此错误 = 这里有什么问题你能举例说明我如何将数据设置为comoboxo plz
    • 检查更新,如果您需要其他内容,请立即联系我
    • 我的getter错误仍然在这里你错过了attrirbute的名字吗? CityList = new ObservableCollection(await _dal.GetCityListAsync());
    • CityList 对象中有一个 CityList 集合,你在哪里使用异步代码实例化集合?
    猜你喜欢
    • 1970-01-01
    • 2011-01-06
    • 1970-01-01
    • 2013-09-20
    • 2011-06-13
    • 2012-06-18
    • 2011-03-09
    相关资源
    最近更新 更多