Can someone guide me how programmatically expand and collapse the tree and subtrees? I currently do not use a property called IsExpand.
My view
<controls:TreeView ItemTemplate="{StaticResource TreeviewDataTemplate}"
ItemsSource="{Binding TreeItems}" Style="{StaticResource TouchTreeViewStyle}"
HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" ScrollViewer.VerticalScrollBarVisibility="Disabled"/>
Data binding on TreeViewpageViewModel:
private void BuildTree()
{
var tree = BuildChildrenTree(_fullAgendaItems.Where(a => a.PreviousId == null).ToList());
TreeItems = tree;
}
and
private ObservableCollection<AgendaItem> BuildChildrenTree(List<AgendaItem> agendaItems)
{
var tree = new ObservableCollection<AgendaItem>();
const string functionName = "BuildChildrenTree";
try
{
//Logs.Write.Entry(ClassName + functionName);
foreach (var item in agendaItems)
{
item.Children =
BuildChildrenTree(
FullAgendaItems.Where(a => a.PreviousId == item.Id && item.HeadorPaper == 0).ToList());//
#region Change bg color of the currently seleted item
if (_globalSelectedAgendaItem != null && _globalSelectedAgendaItem.Id == item.Id)
{
item.AgendaItemDefaultBg = SelectedColor;
}
#endregion
tree.Add(item);
}
//Logs.Write.Success(ClassName + functionName);
}
catch (Exception ex)
{
Logs.Write.Error(Utility.FmtErrData(ClassName + functionName, ex));
}
return tree;
}
For
TreeViewinWinRTXamlToolkit, everyTreeViewItemin theTreeViewhas theIsExpandproperty. You could get theTreeViewItemyou want to expand or collapse code behind and setIsExpandproperty for it.To use
ContainerFromItemmethod to get theTreeViewItemby the items you bind to theTreeView, in your code snippet should beAgendaItem.Suppose your
TreeViewis named withtvDataBound, the following code snippet will expand the first item.By the way, the official sample provide a
TreeViewyou could also reference, it is settingExpandproperty withTreeViewNode. Also in the insider preview, UWP app provide aTreeViewcontrol.