0
votes

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. 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:

    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:

<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.

2

2 Answers

0
votes

You need to implement INotifyPropertyChanged interface on your ViewModel class, like this:

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

You need to implement property changed notification. When the project is changed, you are assigning a new CollectionView to Tasks. In order for the UI to know about it, you should raise the PropertyChanged event.

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