WinRT XAML工具包TreeView保存状态

时间:2016-04-08 15:40:42

标签: treeview winrt-xaml-toolkit

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

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

谢谢。

1 个答案:

答案 0 :(得分:0)

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

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");
            }            
        }
    }
}

然后,您可以使用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将导航与菜单分开,因此您无需保存菜单的状态。

<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>