簡體   English   中英

如何以編程方式展開和折疊WinRTXamlToolkit Treeview?

[英]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;
    }

對於TreeViewWinRTXamlToolkit ,每一個TreeViewItemTreeViewIsExpand財產。 您可以獲取要擴展或折疊代碼的TreeViewItem ,並IsExpand設置IsExpand屬性。

要使用ContainerFromItem方法來獲取TreeViewItem通過綁定到該項目TreeView ,在你的代碼段應AgendaItem

假設您的TreeViewtvDataBound命名,下面的代碼片段將展開第一項。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM