Come in order then. xaml:
<TreeView ItemsSource="{Binding Nodes}"> <TreeView.ItemTemplate> <HierarchicalDataTemplate DataType="{x:Type local:INode}" ItemsSource="{Binding Nodes}"> <TextBlock Margin="2" Text="{Binding Path=Name}" /> </HierarchicalDataTemplate> </TreeView.ItemTemplate> </TreeView>
DataContext window accordingly:
public class MainViewModel : ViewModelBase { #region Private Fields private readonly IDataService _dataService; private INode _selectedNode; #endregion Private Fields #region Public Constructors /// <summary> /// Отправная точка, главная ViewModel. /// </summary> public MainViewModel(IDataService dataService) { Contract.Assert(dataService != null); _dataService = dataService; } #endregion Public Constructors #region Public Properties public List<INode> Nodes { get; private set; } public INode SelectedNode { get { return _selectedNode; } set { if (_selectedNode != value) { _selectedNode = value; RaisePropertyChanged(); } } } #endregion Public Properties }
and INode itself:
public interface INode { #region Public Properties string Name { get; } List<INode> Nodes { get; } #endregion Public Properties }
accordingly, it is necessary to add to them a standard binding in the form of a service locator (that would push the context to the window or explicitly do this) and the implementation of the INode interface. Everything else depends on the tasks. Selected node, cascading, etc.