且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将observerbaleCollection绑定到wpf组合框

更新时间:2022-10-21 13:49:06

基本上,你必须使用 ItemSource SelectedValuePath DisplayMemberPath 具有相应绑定的属性。

 <   combobox     itemssource   =  {Binding Path = AvailableTypes} >  
SelectedValuePath =[您选择的值路径]
DisplayMemberPath = [您的展示会员路径]/>
< / combobox > 跨度>


I''m having Class call Apps.It has Observable collection called AvailableTypes.I want to bind this AvailableTypes observable collection to the wpf Combo Box.when form is loaded these AppId should loaded into combo box.. Would you give me a solution to this one?

class Apps: INotifyPropertyChanged{

        ServiceReference1.AssetManagerServiceClient client;
        ObservableCollection<string> availableType;

        public ObservableCollection<string> AvailableTypes
        {
            get
            {
                if (availableType == null)
                {
                    availableType = new ObservableCollection<string>();

                }
                client = new ServiceReference1.AssetManagerServiceClient();
                List<string> AssestList = client.GetAppIds().ToList<string>();

                foreach (string appid in AssestList)
                {
                    availableType.Add(appid);
                }

                return availableType;
            }

            set
            {
                availableType = value;
                NotifyPropertyChanged("AvailableTypes");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

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

Basically, you have to use "ItemSource", "SelectedValuePath", "DisplayMemberPath" property with respective binding.
<combobox itemssource="{Binding Path=AvailableTypes}">
          SelectedValuePath="[your selected value path]"
          DisplayMemberPath="[your display member path]" />
</combobox>