使用HierarchicalDataTemplate可以显示结构化数据,而无需将父类与子对象的列表或集合一起使用

时间:2019-01-10 18:14:25

标签: wpf xaml treeview hierarchicaldatatemplate

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

我正在寻找本期的MVVM XAM1解决方案,以从具有树形视图的视图中的视图模型中加载视图模型中的数据,并在视图中使用尽可能少的代码。

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

Screen shot Winforms app

简化的数据实体类。

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;}
}

1 个答案:

答案 0 :(得分:0)

放弃了我的方法,并根据代码项目示例采用了一种方法: 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()
    {...}           
}

公共类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
    {...}

}