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>
有一种方法可以保存树视图的状态(展开和选定的属性)以便在应用程序的导航和墓碑期间保持状态?
我不想在 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>