I want to use WPF treeview with one or more HierarchicalDataTemplates to create a treeview displaying Locations organized by Country, State and the location.
I am looking for a MVVM XAMl solution in this issue to load the data from the viewmodel in a view with a treeview with as little code as possible in the view itself.
Sample screen shot from existing winforms application.
Simplified data entity classes.
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;}
}
Abandoned my approach and went with one based on the codeproject example: 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
{...}
}