树视图分层DataTemplate绑定 - MVVM

时间:2017-09-29 09:53:20

标签: c# wpf mvvm data-binding treeview

这里我试图将“解决方案”列表绑定到TreeView。每个“解决方案”都有“文件”列表和“解决方案名称”。我想使用Hierarchical DataTemplate来执行此操作。在调试模式下,我检查了“解决方案”列表和“文件”列表是否已成功设置。但在我看来,没有任何表现。 另外,在我的视图类中,当我尝试设置Hierarchical DataTemplate的数据类型时,它说“SolutionExplorerModel”在命名空间中不存在,即使它确实存在。

视图模型

public class SolutionExplorerViewModel : INotifyPropertyChanged
    {
        private List<SolutionExplorerModel> _solutions = new List<SolutionExplorerModel>();
        public List<SolutionExplorerModel> Solutions
        {
            get { return _solutions; }
            set
            {
                _solutions = value;
                RaisePropertyChanged("Solutions");
            }
        }

        public SolutionExplorerViewModel()
        {
            Messenger.Default.Register<OpenFileDialog>(this, OnItemReceived);
        }

        private void OnItemReceived(OpenFileDialog openFile)
        {
            var solutionName = openFile.SafeFileName.Replace(".psim", "");
            var files = new List<FileModel>();
            var solutionPath = openFile.FileName.Replace(openFile.SafeFileName, "");
            foreach(var file in Directory.EnumerateFiles(solutionPath, "*.xml"))
            {
                files.Add(new FileModel(file));
            }
            var newSolution = new SolutionExplorerModel
            {
                SolutionName = solutionName,
                Files = files
            };
            _solutions.Add(newSolution);
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propertyThatChanged)
        {
            //checking if event is not null than raise event and pass
            //in propperty name that has changed
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyThatChanged));
        }
    }

SolutionExplorerModel

public class SolutionExplorerModel : INotifyPropertyChanged
    {
        private string _solutionName;
        public string SolutionName
        {
            get { return _solutionName; }
            set
            {
                _solutionName = value;
                RaisePropertyChanged("SolutionName");
            }
        }

        private List<FileModel> _files;
        public List<FileModel> Files
        {
            get { return _files; }
            set
            {
                _files = value;
                RaisePropertyChanged("Files");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void RaisePropertyChanged(string propertyThatChanged)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyThatChanged));
        }
    }

FileModel

public class FileModel : INotifyPropertyChanged
{
    private string _safeName;
    public string SafeName
    {
        get { return _safeName; }
        set
        {
            _safeName = value;
            RaisePropertyChanged("SafeName");
        }
    }

    private string _path;
    public string Path
    {
        get { return _path; }
        set
        {
            _path = value;
            RaisePropertyChanged("Path");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void RaisePropertyChanged(string propertyThatChanged)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyThatChanged));
    }

    public FileModel(string path)
    {
        this.Path = path;
        this.SafeName = path.Split('\\').LastOrDefault();
    }
}

视图

 <TreeView ItemsSource="{Binding Solutions}" DataContext="{Binding Source={StaticResource mainViewModelLocater}, Path=SolutionExplorerViewModel}">
            <TreeView.Resources>
                <HierarchicalDataTemplate DataType="{x:Type model:SolutionExplorerModel}" ItemsSource="{Binding Files}">
                    <TextBlock Text="{Binding SolutionName}"></TextBlock>
                </HierarchicalDataTemplate>
            </TreeView.Resources>
        </TreeView>

1 个答案:

答案 0 :(得分:0)

如果您SolutionExplorerViewModel的{​​{1}}属性实际返回已填充的mainViewModelLocater,则此方法应该有效:

SolutionExplorerViewModel

尝试明确设置<TreeView ItemsSource="{Binding Solutions}" DataContext="{Binding Source={StaticResource mainViewModelLocater}, Path=SolutionExplorerViewModel}"> <TreeView.Resources> <HierarchicalDataTemplate DataType="{x:Type model:SolutionExplorerModel}" ItemsSource="{Binding Files}"> <TextBlock Text="{Binding SolutionName}"></TextBlock> </HierarchicalDataTemplate> <DataTemplate DataType="{x:Type model:FileModel}"> <TextBlock Text="{Binding SafeName}" /> </DataTemplate> </TreeView.Resources> </TreeView> 并确保填充DataContext集合:

Solutions