<syncfusion:TreeViewAdv x:Name="TreeViewone" ItemsSource="{Binding TreeItems}" SelectedTreeItem="{Binding SelectedTreeItem, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Row="0">
<syncfusion:TreeViewAdv.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding SubItems}">
<TextBlock Text="{Binding Header}" />
</HierarchicalDataTemplate>
</syncfusion:TreeViewAdv.ItemTemplate>
</syncfusion:TreeViewAdv>
|
public class ViewModel : INotifyPropertyChanged
{
public commandclass TraverseSelectedTreeItem { get; set; }
public ViewModel()
{
TreeItems = new ObservableCollection<Model>();
PopulateData();
//Intialize the command
this.TraverseSelectedTreeItem = new commandclass(CanTraverseSelectedTreeItem, ExecuteTraverseSelectedTreeItem);
}
//Execute when the button clicks
private void ExecuteTraverseSelectedTreeItem(object obj)
{
TreeViewAdv item = obj as TreeViewAdv;
if (item.SelectedTreeItem == TreeItems[0])
{
item.ClearSelection();
item.SelectedTreeItem = TreeItems[1];
}
}
private bool CanTraverseSelectedTreeItem(object obj)
{
return true;
}
private object selectedtreeitem;
public object SelectedTreeItem
{
get
{
return selectedtreeitem;
}
set
{
selectedtreeitem = value;
NotifyPropertyChanged("SelectedTreeItem");
}
}
public ObservableCollection<Model> TreeItems { get; set; }
//Used to populate the items
private void PopulateData()
{
Model Root1 = new Model() { Header = "Parent1" };
PopulateSubItems(Root1);
TreeItems.Add(Root1);
Model Root2 = new Model() { Header = "Parent2" };
PopulateSubItems(Root2);
TreeItems.Add(Root2);
Model Root3 = new Model() { Header = "Parent3" };
PopulateSubItems(Root3);
TreeItems.Add(Root3);
Model Root4 = new Model() { Header = "Parent4" };
PopulateSubItems(Root4);
TreeItems.Add(Root3);
Model Root5 = new Model() { Header = "Parent5" };
PopulateSubItems(Root5);
TreeItems.Add(Root3);
}
//Used the Populate the subitems
private void PopulateSubItems(Model Root)
{
Model SubItem1 = new Model() { Header = Root.Header + " Item1" };
Model SubItem2 = new Model() { Header = Root.Header + " Item2" };
Model SubItem3 = new Model() { Header = Root.Header + " Item3" };
Model SubItem4 = new Model() { Header = Root.Header + " Item4" };
Root.SubItems.Add(SubItem1);
Root.SubItems.Add(SubItem2);
Root.SubItems.Add(SubItem3);
Root.SubItems.Add(SubItem4);
}
//INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
|