使用HierarchicalDataTemplate显示结构化数据,而不使用具有列表或子对象集合的父类

问题描述 投票:0回答:1

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

我正在寻找一个MVVM XAMl解决方案来解决这个问题,在视图本身中使用尽可能少的代码在树视图中加载来自viewmodel的数据。

从现有的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;}
}
wpf xaml treeview hierarchicaldatatemplate
1个回答
0
投票

放弃了我的方法,并根据codeproject示例: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 {readonly County _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
    {...}

}
© www.soinside.com 2019 - 2024. All rights reserved.