当前位置:   article > 正文

WPF MVVM 绑定ComboBox数据_mvvm中点击按钮更新combox绑定值

mvvm中点击按钮更新combox绑定值

一、属性变更基类 NotificationBase.cs

  1. public class NotificationBase : INotifyPropertyChanged
  2. {
  3. public event PropertyChangedEventHandler PropertyChanged;
  4. public void NotifyPropertyChanged(string propertyName)
  5. {
  6. if (this.PropertyChanged != null)
  7. {
  8. this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  9. }
  10. }
  11. }

  1. [DataContract]
  2. public abstract class BaseModel : NotificationBase
  3. {
  4. }


二、ViewModel层

    public class PageBuildProductsModel : BaseModel
    {


        //产品列表
        private List<ProductsModel> productslist;
        public List<ProductsModel> ProductsList
        {
            get { return productslist; }
            set
            {
                productslist = value;
                NotifyPropertyChanged("ProductsList");
            }
        }
        //选中产品
        public ProductsModel CurrentProducts { get; set; }  


        public PageBuildProductsModel()
        {


            ProductsList = new List<ProductsModel>();
            foreach (.....)
            {
                ProductsList.Add(......);
            }


            this.CurrentProducts = this.ProductsList[0]; 
        }


        private void Build(object obj)
        {

            //获取选中的值
            MessageBox.Show(CurrentProducts.Queries);
        }
    }


三、View 

<ComboBox ItemsSource="{Binding ProductsList}" 

Width="100" 

DisplayMemberPath="Name" 

SelectedItem="{Binding CurrentProducts, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>


注意:当前选项,绑定CurrentProducts




声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/928541
推荐阅读
相关标签
  

闽ICP备14008679号