【问题标题】:Update itemssource in binding to combobox using MVVM pattern in WPF在 WPF 中使用 MVVM 模式更新绑定到组合框的 itemssource
【发布时间】:2015-07-02 22:46:22
【问题描述】:

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

    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>

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

【问题讨论】:

    标签: c# wpf mvvm data-binding


    【解决方案1】:

    您需要实现属性更改通知。更改项目时,您将新的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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2018-07-16
        • 2015-09-15
        • 1970-01-01
        • 2021-10-11
        • 2015-07-11
        • 2015-04-27
        • 2017-07-14
        • 2023-03-18
        • 2011-03-13
        相关资源
        最近更新 更多