我正在使用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);
}
}
你重新創建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