在数据重新加载时保存WPF TreeView状态
问题描述:
我正在使用TreeView在UI中显示我的数据。现在我的应用程序每5秒刷新一次,以显示最新的数据。即使在窗口重新加载后,我是否可以保存我的展开状态或折叠状态的树状视图?因为如果我有大量的数据,并且需要超过5秒的时间才能找到所需的数据,那么TreeView会在每5秒刷新一次窗口后崩溃,我必须从头开始。在数据重新加载时保存WPF TreeView状态
<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);
}
}
答
我解决了这个问题,通过增加IsExpanded和IsSelected属性的对象,我的TreeView注定要
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
</Style>
你重新创建Sections集合每5秒? – Rachel 2010-10-21 19:00:55
是的..窗口每5秒重新加载/刷新一次。所以我把它当作一个可观察的集合,被清除,然后用对象填充。 – developer 2010-10-21 19:24:32
哦,那么你的IsExpanded属性也每5秒清除一次。你有可能只更新值而不是删除它们并重新创建它们? – Rachel 2010-10-21 19:25:48