简体   繁体   English

使用WPF中的MVVM模式更新绑定到组合框的itemssource

[英]Update itemssource in binding to combobox using MVVM pattern in WPF

I am developing a WPF application using MVVM pattern, in that application i have two comboboxes, i bind the itemssource of the comboboxes from a property of viewmodel, thoses properties are of type CollectionView class which implements ICollectionView interface. 我正在使用MVVM模式开发WPF应用程序,在该应用程序中我有两个组合框,我从viewmodel的属性绑定组合框的itemssource,这些属性是实现ICollectionView接口的CollectionView类类型。 For tracking the current selected item. 用于跟踪当前选定的项目。 Because i need to update the second combobox items depending on the value of the selected item in the first combobox. 因为我需要根据第一个组合框中所选项目的值来更新第二个组合框项目。 This is a snapshot from the viewmodel class code: 这是来自viewmodel类代码的快照:

    public ICollectionView Projects { get; set; }

    public ICollectionView Tasks { get; set; }

    public ICollectionView Users { get; set; }
public NewTaskViewModel()
    {
        Projects = new CollectionView(this.GetProjects());
        Projects.CurrentChanged += new EventHandler(projects_CurrentChanged);
        Users = new CollectionView(this.GetProjectAssignedUsers());
    }

    void projects_CurrentChanged(object sender, EventArgs e)
    {
        Api.Project project = Projects.CurrentItem as Api.Project;
        this.SelectedProjectId = project.Id;
        this.Tasks = new CollectionView(this.GetTaskLists());
    }

And this is the XAML part: 这是XAML部分:

<ComboBox x:Name="textBox3" Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" ItemsSource="{Binding Path=Projects, Mode=TwoWay}" DisplayMemberPath="Name" IsSynchronizedWithCurrentItem="True">
        </ComboBox>


        <ComboBox x:Name="textBox4" Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="2" ItemsSource="{Binding Path=Tasks}" DisplayMemberPath="Name" IsSynchronizedWithCurrentItem="True">
        </ComboBox>

What i am doing wrong because i don't get the second combo updated when i change the current selected item. 我做错了什么,因为当我更改当前所选项目时,我没有更新第二个组合。 I hope for a little help. 希望能有所帮助。 cheers. 干杯。

You need to implement property changed notification. 您需要实施属性更改通知。 When the project is changed, you are assigning a new CollectionView to Tasks . 更改项目后,您将为Tasks分配一个新的CollectionView In order for the UI to know about it, you should raise the PropertyChanged event. 为了让UI知道它,您应该引发PropertyChanged事件。

public class MyClass : INotifyPropertyChanged
    {
        private ICollectionView tasks;
        public ICollectionView Tasks
        {
            get
            {
                return tasks;
            }
            set
            {
                tasks = value;
                OnPropertyChanged("Tasks");
            }
        }

        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }

Refer this link for more details - https://msdn.microsoft.com/en-us/library/ms743695(v=vs.110).aspx 请参阅此链接以获取更多详细信息-https://msdn.microsoft.com/zh-cn/library/ms743695(v=vs.110).aspx

You need to implement INotifyPropertyChanged interface on your ViewModel class, like this: 您需要在ViewModel类上实现INotifyPropertyChanged接口,如下所示:

    public class NewTaskViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
        public ICollectionView Projects { get; set; }
        private ICollectionView _Tasks;
        public ICollectionView Tasks
        {
          get {return _Tasks;}
          set
          {
              if(_Tasks != value)
              {
                 _Tasks = value;
                 OnPropertyChanged("Tasks");
              }
          }
        }

        public ICollectionView Users { get; set; }
        public NewTaskViewModel()
        {
        Projects = new CollectionView(this.GetProjects());
        Projects.CurrentChanged += new EventHandler(projects_CurrentChanged);
             Users = new CollectionView(this.GetProjectAssignedUsers());
        }

        void projects_CurrentChanged(object sender, EventArgs e)
        {
             Api.Project project = Projects.CurrentItem as Api.Project;
             this.SelectedProjectId = project.Id;
             this.Tasks = new CollectionView(this.GetTaskLists());
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM