Save WPF TreeView state when reloading data

I use TreeView to display my data in the user interface. Now my application is updated every 5 seconds to display the latest data. Is there a way to keep my extended state or minimized state of treeview even after the window reloads? Since if I have a huge amount of data and I take more than 5 seconds to go to the desired data, the TreeView just resets every 5 seconds with a window update, 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);
        }
      }
+5
source share
1 answer

I solved this problem by adding the IsExpanded and IsSelected properties to the object my TreeView contacted with

<Style TargetType="{x:Type TreeViewItem}">
    <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
</Style>
+13
source

All Articles