在数据重新加载时保存 WPF TreeView 状态 [英] Save WPF TreeView state on data reload
问题描述
我正在使用 TreeView 在 UI 中显示我的数据.现在我的应用程序每 5 秒刷新一次,以便显示最新的数据.有没有办法即使在窗口重新加载后也可以保存树视图的展开状态或折叠状态?因为如果我有大量的数据并且我花了超过 5 秒的时间来访问所需的数据,则 TreeView 在每 5 秒刷新一次窗口后就会崩溃,我必须从头开始.
I am using TreeView to display my data in UI. Now my application refreshes every 5 seconds so that it shows the most current data. Is there a way I can save my expanded state or collapsed state of treeview even after window reload? Because if I have a huge amount of data and I take more than 5 seconds to go to desired data, the TreeView just collapses after every 5 seconds with window refresh, and I have to start from scratch.
<TreeView ItemsSource="{Binding Sections}" Grid.Row="1"
ItemTemplate="{StaticResource sectionTemplate}" >
<TreeView.Resources>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
</Style>
</TreeView.Resources>
</TreeView>
public ObservableCollection<MyViewModel> =new ObservableCollection<MyViewModel>();
public bool IsExpanded
{
get { return (bool)GetValue(IsExpandedProperty); }
set { SetValue(IsExpandedProperty, value); }
}
public static readonly DependencyProperty IsExpandedProperty = DependencyProperty.Register("IsExpanded", typeof(bool), typeof(MyViewModel));
if (result.TotalResults > 0)
{
foreach (DomainObject obj in result.ResultSet)
{
AT myAT= (AT)obj;
arrdep.Add(myAT);
}
}
推荐答案
我通过向 TreeView 绑定到的对象添加 IsExpanded 和 IsSelected 属性解决了这个问题
I solved that problem by adding IsExpanded and IsSelected properties to the object that my TreeView was bound to
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
</Style>
这篇关于在数据重新加载时保存 WPF TreeView 状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!