我正在使用 TreeView 在 UI 中显示我的数据。现在,我的应用程序每 5 秒刷新一次,以便显示最新的数据。有没有办法即使在窗口重新加载后也可以保存树视图的展开状态或折叠状态?因为如果我有大量数据并且需要超过 5 秒才能转到所需数据,则 TreeView 在每 5 秒刷新一次窗口后就会崩溃,我必须从头开始。
<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 属性解决了这个问题
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
</Style>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)