private ObservableCollection<ProjectItem> _projectItems;
public ObservableCollection<ProjectItem> DataItems
{
get { return _projectItems; }
}
public ProjectsPanelViewViewModel()
{
_projectItems = new ObservableCollection<ProjectItem>();
_createProjectCommand = new DelegateCommand(OnCreateProjectCommand);
_saveProjectCommand = new DelegateCommand(OnSaveProjectCommand);
}
private void OnCreateProjectCommand()
{
_addProjectViewModel = new AddProjectViewModel();
AddProjectView view = new AddProjectView();
view.Show();
}
private void OnSaveProjectCommand()
{
ProjectItemViewModel _vm = new ProjectItemViewModel();
_vm.ProjectName = ProjectName;
_vm.ProjectDescription = ProjectDescription;
var item = new ProjectItem(_vm);
_projectItems.Add(item);
}
XAML
<UserControl.DataContext>
<VM:ProjectsPanelViewViewModel/>
</UserControl.DataContext>
<StackPanel Name="ProjectsPanel" Style="{DynamicResource FullLengthPanelStyle}" >
<StackPanel Name="ProjectsHeader" Orientation="Horizontal" Margin="0,0,0,5">
<Label Style="{StaticResource TitleLabelStyleBlue}">projects</Label>
<Button Name="AddProjects" Margin="10,5,0,0" Style="{StaticResource CircularGreyButtonStyle}" Content="+" ToolTip="Add a new project" Command="{Binding CreateProjectCommand}"/>
<Button Name="ExpandProjects" Margin="10,5,0,0" Style="{StaticResource CircularGreyButtonStyle}" Content=">" ToolTip="Expand projects"/>
</StackPanel>
<ScrollViewer VerticalScrollBarVisibility="Visible">
<ItemsControl ItemsSource="{Binding DataItems}"/>
</ScrollViewer>
</StackPanel>
当 OnCreateProjectCommand 打开时,它会创建一个包含 2 个字符串字段的窗口,然后有一个执行 OnSaveProjectCommand 的按钮。命令执行得很好,但是 UI 没有发现新项目已添加到集合中。(调试时它就在那里)。但是,如果您更改 OnSaveProjectCommand 中的代码并将其放入 OnCreateProjectCommand 中,并对简单字符串值进行硬编码,则 UI 会从 observable 集合中正确更新。
谁能给我一些关于为什么这不起作用的指导,以及我应该从这个命令产生一个新窗口的正确方法。这应该通过棱镜的基于状态的视图导航来完成吗?我们已经将其用于模块化和指挥。
感谢您的建议。