Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 623065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:03:08+00:00 2026-05-13T19:03:08+00:00

This code example shows how to get the position of the scrollbar in a

  • 0

This code example shows how to get the position of the scrollbar in a ScrollViewer (with ScrollToVerticalOffset) so that e.g. you can reset the scrollbar to this position if you need to recreate it.

Is there any way to do this for a TreeView control, i.e. get a collection of node indexes which the user has expanded?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:03:08+00:00Added an answer on May 13, 2026 at 7:03 pm

    The easiest way to do this is to bind TreeViewItem.IsExpanded property to the ViewModel, and then go through model and calculate.

    I wrote an example for you. It calculates number of expanded nodes, but you can do whatever you want with expanded guys…

    C#:

    using System;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Windows;
    
    namespace WpfApplication1
    {
      public partial class TestBrowser : Window
      {
        private TreeViewItemViewModel[] _items;
    
        public TestBrowser()
        {
          InitializeComponent();
    
            var item1 = new TreeViewItemViewModel();
            var item2 = new TreeViewItemViewModel();
            var item3 = new TreeViewItemViewModel();
    
            item3.Children.Add(new TreeViewItemViewModel());
            item3.Children.Add(new TreeViewItemViewModel());
            var child3 = new TreeViewItemViewModel();
            child3.Children.Add(new TreeViewItemViewModel());
            item3.Children.Add(child3);
    
            _items = new[] {item1, item2, item3};
            tv.DataContext = _items;
        }
    
            private void CalculateExpandedClick(object sender, RoutedEventArgs e)
            {
                var expanded = 0;
                foreach (TreeViewItemViewModel item in _items)
                {
                    expanded += GetNumberOfExpanded(item);
                }
                ExpandedNumber.Text = expanded.ToString();
            }
    
        private int GetNumberOfExpanded(TreeViewItemViewModel model)
        {
            var expandedCount = 0;
            if (model.IsExpanded)
            {
                expandedCount += 1;
                foreach (TreeViewItemViewModel child in model.Children)
                {
                    expandedCount += GetNumberOfExpanded(child);
                }
            }
            return expandedCount;
        }
      }
    
        /// <summary>
        /// Single tree view item view model.
        /// </summary>
        public class TreeViewItemViewModel : INotifyPropertyChanged
        {
            public ObservableCollection<TreeViewItemViewModel> Children
            {
                get; private set;
            }
    
            private bool _isExpanded;
            private string _text;
    
            public bool IsExpanded
            {
                get { return _isExpanded; }
                set
                {
                    _isExpanded = value;
                    OnPropertyChanged("IsExpanded");
                }
            }
    
            public string Text
            {
                get { return _text; }
                set
                {
                    _text = value;
                    OnPropertyChanged("Text");
                }
            }
    
            public TreeViewItemViewModel()
            {
                Children = new ObservableCollection<TreeViewItemViewModel>();
                Text = DateTime.Now.ToLongTimeString(); // Just fake data.
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            private void OnPropertyChanged(string name)
            {
                var changed = PropertyChanged;
                if (changed != null)
                {
                    changed(this, new PropertyChangedEventArgs(name));
                }
            }
        }
    }
    

    XAML:

    <Window x:Class="WpfApplication1.TestBrowser"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Expanded Test"
            Height="300"
            Width="300">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <TreeView x:Name="tv"
                  ItemsSource="{Binding}">
                <TreeView.ItemContainerStyle>
                    <Style TargetType="{x:Type TreeViewItem}">
                        <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                    </Style>
                </TreeView.ItemContainerStyle>
                <TreeView.ItemTemplate>
                    <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                        <TextBlock Text="{Binding Text}" />
                    </HierarchicalDataTemplate>
                </TreeView.ItemTemplate>
            </TreeView>
    
            <StackPanel Grid.Column="1">
                <Button Content="Get number of expanded items" 
                                Click="CalculateExpandedClick"/>
                <TextBlock x:Name="ExpandedNumber" />
            </StackPanel>
        </Grid>
    </Window>
    

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there anyway I can modify this code example #include <stdlib.h> #include <iostream> class
I have this code (an example that PHP generates for me)... <select id=outsideBlue3> <option
In this example code, I'm trying to offset the Grid 's Canvas position by
In this very simple code example, messages get lost every once in a while.
we have this example: http://code.google.com/apis/maps/documentation/v3/basics.html#DetectingUserLocation which shows how to get the user's position in
My brother wrote this code for me to show an example of polymorhism. But
For this code example below, usually I would use [self fall]; instead of the
Take this example code (ignore it being horribly inefficient for the moment) let listToString
In this example code it deals with framebuffers before setting up the context. I've
Take this code snippet for example: window.location.href = 'mysite.htm#images'; Already being on the site

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.