簡體   English   中英

WinRT XAML工具包TreeView的保存狀態

[英]WinRT XAML Toolkit TreeView Save state

有沒有一種方法可以保存樹視圖的狀態(擴展和選定的屬性),以便在導航和應用程序邏輯刪除期間保持狀態?

我不想在itemsource上添加此類信息,因為語義上是兩個不同的方法。 ItemSource是與擴展狀態沒有任何關系的域對象。

謝謝。

您可以將這些信息保存在與樹的每個節點關聯的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");
            }            
        }
    }
}

然后,您可以將這些屬性與視圖綁定。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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