TreeView 和 Prism-5 的问题



我在尝试使用 Prism 和 MVVM 构建 TreeView 时遇到了很多问题。我将我的应用程序划分为区域,其中一个区域具有带有 TreeView 的模块,另一个区域具有功能区作为模块,可以在其中创建和配置某种项目。这些区域使用棱镜进行通信,并且效果很好。因此,当在"功能区模块"中创建项目时,"TreeView 模块"会得到它,这就是问题所在。

"TreeView Module"的 ViewModel 有一个 ObservableCollection,其中应添加所有项目。项目的名称和此类的其他属性应显示在树视图中。

public class Project : BindableBase
{
    private List<DataSet> _DataSetList;
    private string _projectName;
    public Project()
    {
        DataSetList = new List<DataSet>();
        ProjectName = "";
    }
    public Project(string projectName, List<DataSet> dataSets)
    {
        ProjectName = projectName;
        DataSetList = dataSets;
    }
    public string ProjectName
    {
        get { return _projectName; }
        set { SetProperty(ref this._projectName, value); }
    }
    public List<DataSet> DataSetList
    {
        get { return _DataSetList; }
        set { SetProperty(ref this._DataSetList, value); }
    }
    public bool CheckForLoadedDataSets() 
    {
        foreach (DataSet ds in DataSetList) 
        {
            if(ds.Status != DataSet.DataSetStatus.Loaded)
            {
                return false;
            }
        }
        return true;
    }
}

这就是数据集

public class DataSet : BindableBase
{
    public enum DataSetStatus
    {
        Loaded,
        Stopped,
        Unloaded,
        Empty,
        LoadingData,
        CorruptData,
    };
    private string _dataSetSource;
    private DataSetStatus _status;
    public DataSet(string name, string sourceName, List<Injector> injertors)
    {
        DataSetName = name;
        DataSetSource = sourceName;
        Injectors = injertors;
        Status = DataSetStatus.Empty;
    }
    public DataSet(string name, string sourceName)
    {
        DataSetName = name;
        DataSetSource = sourceName;
        Injectors = new List<Injector>();
        Status = DataSetStatus.Empty;
    }
    public DataSet(string sourceName)
    {
        DataSetName = "";
        DataSetSource = sourceName;
        Injectors = new List<Injector>();
        Status = DataSetStatus.Empty;
    }
    public string DataSetName { get; set; }
    public string DataSetSource { get { return _dataSetSource; } set { SetProperty(ref this._dataSetSource, value); } }
    public List<Injector> Injectors { get; set; }
    public DataSetStatus Status { get { return _status;} set{ SetProperty(ref this._status,value);} }
}

这是树视图模型

class TreeProjectManagerViewModel : BindableBase, INavigationAware
{
    private ObservableCollection<Project> _projectCollection;        
    public ObservableCollection<Project> Projects
    {
        get { return this._projectCollection; }
        set 
        {
            SetProperty(ref this._projectCollection, value); 
        }
    }
    public TreeProjectManagerViewModel() 
    {
        Projects = new ObservableCollection<Project>();                        
    }
    /// <summary>
    /// Checking parameters
    /// </summary>
    /// <param name="navigationContext"></param>
    /// <returns></returns>
    public bool IsNavigationTarget(NavigationContext navigationContext)
    {
        if (navigationContext.Parameters["ProjectObject"] != null) 
        {
            return true;
        }                      
        return false;
    }
    public void OnNavigatedFrom(NavigationContext navigationContext)
    {
    }
    /// <summary>
    /// Getting information from other module 
    /// </summary>
    /// <param name="navigationContext"></param>
    public void OnNavigatedTo(NavigationContext navigationContext)
    {
        Project p = (Project)navigationContext.Parameters["ProjectObject"];            
        Projects.Add(p);
    }
}

这里是视图中的代码

<Grid>
    <TreeView  ItemsSource="{Binding Projects}">            
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Path=Projects}">
                <TextBlock Text="{Binding Path=ProjectName}"/>                    
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>
</Grid>

有了这个,我可以在我的树视图中获取项目的名称,但例如,这里有我的问题:

1.- 如何获得具有每个数据集名称的树节点的第二个层次结构?

2.- 如何管理树视图中的事件?

提前非常感谢你。

尝试类似的事情:

<TreeView ItemsSource="{Binding Projects}">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Loaded">
            <ie:CallMethodAction MethodName="OnTreeviewLoaded" TargetObject="{Binding}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate DataType="{x:Type model:Project}" ItemsSource="{Binding Path=DataSetList}">
            <TextBlock Text="{Binding Path=ProjectName}" />
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type model:DataSet}">
            <TextBlock Text="{Binding Path=DataSetName}" />
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

您还需要添加:

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ie="http://schemas.microsoft.com/expression/2010/interactions"

到 XAML 中并在视图模型中创建事件处理程序

最新更新