我在尝试使用 Prism 和 MVVM 构建 TreeView 时遇到了很多问题。我将我的应用程序划分为多个区域,其中一个区域有一个带有 TreeView 的模块,另一个区域有一个功能区作为模块,可以在其中创建和配置某种项目。这些区域使用棱镜进行通信,效果很好。因此,当在“功能区模块”中创建项目时,“树视图模块”会得到它,这就是问题所在。
“TreeView 模块”的 ViewModel 有一个 ObservableCollection,应该在其中添加所有项目。项目名称和此类的其他属性应显示在 TreeView 中。
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;
}
}
这就是 DataSet
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);} }
}
这是 TreeViewViewModel
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>
有了这个,我可以在我的 TreeView 中获取项目的名称,但例如,这里是我的问题:
1.- 如何获得具有每个 DataSet 名称的 TreeNodes 的第二层次结构?
2.- 如何管理 TreeView 中的事件?
非常感谢您提前。