【问题标题】:Tree View Hierarchical DataTemplate Binding - MVVM树视图分层 DataTemplate 绑定 - MVVM
【发布时间】:2017-09-29 09:53:20
【问题描述】:

我在这里尝试将“解决方案”列表绑定到 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));
        }
    }

文件模型

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>

【问题讨论】:

    标签: c# wpf mvvm data-binding treeview


    【解决方案1】:

    如果您的mainViewModelLocaterSolutionExplorerViewModel 属性实际上返回填充的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 集合:

    treeView.DataContext = new SolutionExplorerViewModel();
    

    【讨论】:

    • 我试过这个,我在更新Solutions 后检查了DataContextDataContext 中的数据是正确的。但是 TreeView 中仍然没有显示任何内容。我认为问题出在视图类中,因为它仍然说名称SolutionExplorerModel 在命名空间中不存在
    • 它编译了吗?
    • 是的,它确实可以编译。
    • 但是视图在运行时仍然是空白的?那么你的数据绑定或者你的数据有问题。
    猜你喜欢
    • 2011-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-26
    • 2013-06-07
    • 2012-08-11
    相关资源
    最近更新 更多