![](/img/trans.png)
[英]WinrtXamlToolkit TreeView ItemContainerStyleSelector not firing
[英]How to Expand and collapse WinRTXamlToolkit Treeview programmatically?
有人可以指導我如何以編程方式擴展和折疊樹和子樹嗎? 我目前不使用名為IsExpand的屬性。
我的觀點
<controls:TreeView ItemTemplate="{StaticResource TreeviewDataTemplate}"
ItemsSource="{Binding TreeItems}" Style="{StaticResource TouchTreeViewStyle}"
HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" ScrollViewer.VerticalScrollBarVisibility="Disabled"/>
TreeViewpageViewModel上的數據綁定:
private void BuildTree()
{
var tree = BuildChildrenTree(_fullAgendaItems.Where(a => a.PreviousId == null).ToList());
TreeItems = tree;
}
和
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;
}
對於TreeView
中WinRTXamlToolkit
,每一個TreeViewItem
中TreeView
有IsExpand
財產。 您可以獲取要擴展或折疊代碼的TreeViewItem
,並IsExpand
設置IsExpand
屬性。
要使用ContainerFromItem
方法來獲取TreeViewItem
通過綁定到該項目TreeView
,在你的代碼段應AgendaItem
。
假設您的TreeView
用tvDataBound
命名,下面的代碼片段將展開第一項。
private void BtnExpand_Click(object sender, RoutedEventArgs e)
{
TreeViewItem item = (TreeViewItem)tvDataBound.ContainerFromItem(tvDataBound.Items[0]);
if (!item.IsExpanded)
{
item.IsExpanded = true;
}
}
順便說一句,官方示例提供了一個TreeView
您也可以參考,它使用TreeViewNode
設置Expand
屬性。 同樣在內部預覽中,UWP應用提供了一個TreeView
控件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.