WinRT XAML 工具包 TreeView 保存状态 [英] WinRT XAML Toolkit TreeView Save state

查看:35
本文介绍了WinRT XAML 工具包 TreeView 保存状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以保存树状视图的状态(展开和选择的属性),以便在导航和应用程序墓碑期间保持状态?

there is a way to save the state of a treeview (expanded and selected properties) in order to keep state during navigation and tombstone of application?

我不想在 itemsource 上添加这样的信息,因为语义上是两个不同的度量.ItemSource 是一个域对象,与展开状态没有任何关系.

I don't want to add such of information on itemsource since semantically are two different metter. ItemSource is a domain object not having any relationship with expanded state.

谢谢.

推荐答案

您可以将这些信息保存在与树的每个节点关联的 ViewModel 中,如下所示:

You can save those informations inside the ViewModel associated with each node of the tree like this :

public class PersonViewModel
{
    readonly List<Person> _children = new List<Person>();
    private bool _isExpanded;

    public IList<Person> Children
    {
        get { return _children; }
    }

    public string Name { get; set; }

    /// <summary>
    /// Gets/sets whether the TreeViewItem 
    /// associated with this object is expanded.
    /// </summary>
    public bool IsExpanded
    {
        get { return _isExpanded; }
        set
        {
            if (value != _isExpanded)
            {
                _isExpanded = value;
                this.OnPropertyChanged("IsExpanded");
            }            
        }
    }
}

然后您可以将这些属性与视图绑定.

Then you can bind those properties with the View.

<TreeView ItemsSource="{Binding Persons}">
    <TreeView.ItemContainerStyle>
        <Style TargetType="{x:Type PersonViewModel}">
        <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="FontWeight" Value="Bold" />
            </Trigger>
        </Style.Triggers>
        </Style>
    </TreeView.ItemContainerStyle>

    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type local:PersonViewModel}" 
                                  ItemsSource="{Binding Children}">
            <StackPanel>
                <TextBlock Text="{Binding Name}" />
            </StackPanel>
        </HierarchicalDataTemplate>

        <DataTemplate DataType="{x:Type local:PersonViewModel}">
            <StackPanel>                
                <TextBlock Text="{Binding Name}" />
            </StackPanel>
        </DataTemplate>
    </TreeView.Resources>
</TreeView>

http://www.codeproject.com/Articles/26288/Simplifying-the-WPF-TreeView-by-Using-the-ViewMode

但是您要寻找的是:

您可能希望使用 ContentPresenter 将导航与菜单分开,这样您就不需要保存菜单的状态.

You probably want to separate the navigation from the menu with the use of a ContentPresenter so you don't need to save the state of the menu.

<Page 
    x:Class="DataCloner.Uwp.Shell"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:DataCloner.Uwp"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:views="using:DataCloner.Uwp.Views"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid>
            <views:TopBarView/>
        </Grid>
        <SplitView x:Name="rootSplitView" Content="{Binding myContentView}" DisplayMode="Inline" IsPaneOpen="True" Grid.Row="1"
                OpenPaneLength="300">
            <SplitView.Pane>
                <views:MenuPanelView/>
            </SplitView.Pane>                
        </SplitView>
    </Grid>
</Page>

这篇关于WinRT XAML 工具包 TreeView 保存状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆