在 WPF 中使用 MVVM 模式更新绑定到组合框的项目源

Update itemssource in binding to combobox using MVVM pattern in WPF

我正在使用 MVVM 模式开发 WPF 应用程序,在该应用程序中我有两个组合框,我从视图模型的 属性 绑定组合框的项目源,这些属性的类型为 CollectionView class 它实现了 ICollectionView 接口。用于跟踪当前选择的项目。因为我需要根据第一个组合框中所选项目的值更新第二个组合框项目。 这是视图模型 class 代码的快照:

    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());
    }

这是 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>

我做错了什么,因为当我更改当前选定的项目时,我没有更新第二个组合。 我希望能得到一点帮助。 干杯。

您需要实施 属性 更改通知。更改项目时,您正在将新的 CollectionView 分配给 Tasks。为了让 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));
            }
        }
    }

请参阅此 link 了解更多详情 - https://msdn.microsoft.com/en-us/library/ms743695(v=vs.110).aspx

您需要在您的 ViewModel class 上实现 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());
        }
}