【发布时间】: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