使用 HierarchicalDataTemplate 显示结构化数据,而无需使用带有子对象列表或集合的父 class

Use HierarchicalDataTemplate to display structured data without using a parent class with list or collection of child objects

我想将 WPF 树视图与一个或多个 HierarchicalDataTemplates 结合使用,以创建一个显示按国家、州和位置组织的位置的树视图。

我正在寻找此问题中的 MVVM XAMl 解决方案,以便在视图本身的代码尽可能少的树视图中从视图模型加载数据。

来自现有 winforms 应用程序的示例屏幕截图。

简化数据实体类。

public class Country
{
    public string name{get;set;}
    public Int32 Id{get;set;}
}
public class State
{
    public string name{get;set;}
    public Int32 Id{get;set;}
}
public class Location
{
    public string name {get;set;}
    public Int32 CountryId {get;set;}
    public Int32 StateId {get;set;}
}

放弃我的方法并使用基于代码项目示例的方法: Simplifying the WPF TreeView by Using the ViewModel Pattern

    public class CountryViewModel : TreeViewItemViewModel
{
    readonly Country _country;

    public CountryViewModel(Country country)
                : base(null, true)
    {
        _country = country;
    }

    public string CountryName
    {
        get { return _country.CountryName; }
    }

    public IEnumerable<State>  States { get; set; }
    public IEnumerable<County> Counties { get; set; }
    public IEnumerable<DRC_SQLITE_Mines.Mine> Mines { get; set; }
    public IEnumerable<DRC_SQLITE_locations.Location> Locations { get; set; }             

    protected override void LoadChildren()
    {...}           

}

public class StateViewModel : TreeViewItemViewModel
{
    readonly State _state;

    public StateViewModel(State state, CountryViewModel parentCountry)
        : base(parentCountry, true)
    {
        _state = state;
    }

    public string StateName
    {
        get { return _state.StateName; }
    }

    public IEnumerable<County> Counties { get; set; }
    public IEnumerable<DRC_SQLITE_Mines.Mine> Mines { get; set; }
    public IEnumerable<DRC_SQLITE_locations.Location> Locations { get; set; }

    protected override void LoadChildren()
    {...}           
}

public class CountyViewModel : TreeViewItemViewModel { 只读县 _county;

    public CountyViewModel(County county, StateViewModel parentState)
        : base(parentState, true)
    {
        _county = county;
    }

    public string CountyName
    {
        get { return _county.CountyName; }
    }

    public IEnumerable<DRC_SQLITE_Mines.Mine> Mines { get; set; }
    public IEnumerable<DRC_SQLITE_locations.Location> Locations { get; set; }

    protected override void LoadChildren()
    {...}

}

public class MineViewModel : TreeViewItemViewModel
{
    public MineViewModel(DRC_SQLITE_Mines.Mine mine, CountyViewModel parentCounty)
        : base(parentCounty, false)
    {
        Mine = mine;
    }
    public MineViewModel(DRC_SQLITE_Mines.Mine mine, StateViewModel parentState)
     : base(parentState, false)
    {
        Mine = mine;
    }
    public MineViewModel(DRC_SQLITE_Mines.Mine mine, CountryViewModel parentCountry)
     : base(parentCountry, false)
    {
        Mine = mine;
    }

    public DRC_SQLITE_Mines.Mine Mine { get; }

    RelayCommand _viewDataCommand;
    public ICommand ViewDataCommand
    {...}

}