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

时间:2015-04-24 10:54:37

标签: c# wpf mvvm data-binding

我正在使用MVVM模式开发WPF应用程序,在该应用程序中我有两个组合框,我从viewmodel的属性绑定组合框的itemssource,thoses属性类型为CollectionView类,它实现ICollectionView接口。用于跟踪当前所选项目。因为我需要根据第一个组合框中所选项目的值更新第二个组合框项目。 这是来自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());
    }

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

我做错了,因为当我更改当前所选项目时,我没有更新第二个组合。 我希望得到一些帮助。 欢呼声。

2 个答案:

答案 0 :(得分:0)

您需要实施属性更改通知。更改项目后,您要为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));
            }
        }
    }

有关详细信息,请参阅此链接 - https://msdn.microsoft.com/en-us/library/ms743695(v=vs.110).aspx

答案 1 :(得分:0)

您需要在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());
        }
}