milosev.com
  • Home
    • List all categories
    • Sitemap
  • Downloads
    • WebSphere
    • Hitachi902
    • Hospital
    • Kryptonite
    • OCR
    • APK
  • About me
    • Gallery
      • Italy2022
      • Côte d'Azur 2024
    • Curriculum vitae
      • Resume
      • Lebenslauf
    • Social networks
      • Facebook
      • Twitter
      • LinkedIn
      • Xing
      • GitHub
      • Google Maps
      • Sports tracker
    • Adventures planning
  1. You are here:  
  2. Home

Auto expand tree view

Details
Written by: Stanko Milosev
Category: WPF
Published: 20 June 2014
Last Updated: 20 June 2014
Hits: 7117

If you want your tree view items to be automatically expanded, try something like this XAML:

<Window x:Class="TreeViewMouseOverExpand.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:viewModel="clr-namespace:TreeViewMouseOverExpand.ViewModel"
        xmlns:treeViewModel="clr-namespace:TreeViewMouseOverExpand.Model"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
            <viewModel:TreeViewMouseOverExpandViewModel x:Key="TreeViewMouseOverExpandViewModel" />
        </Grid.Resources>
        
        <TreeView DataContext="{StaticResource TreeViewMouseOverExpandViewModel}" ItemsSource="{Binding TreeViewMouseOverExpandItems}">

            <TreeView.ItemTemplate>

                <HierarchicalDataTemplate ItemsSource="{Binding Children}" DataType="{x:Type treeViewModel:TreeViewMouseOverExpandModel}" >
                    
                    <TreeViewItem Header="{Binding Name}" IsExpanded="False"/>

                </HierarchicalDataTemplate>

            </TreeView.ItemTemplate>

            <TreeView.ItemContainerStyle>
                <Style TargetType="TreeViewItem">
                    <Setter Property="IsExpanded" Value="True" />
                </Style>
            </TreeView.ItemContainerStyle>

        </TreeView>
    </Grid>
</Window>

Notice lines:

<TreeView.ItemContainerStyle>
	<Style TargetType="TreeViewItem">
		<Setter Property="IsExpanded" Value="True" />
	</Style>
</TreeView.ItemContainerStyle>

Expand tree view items in code behind using animation

Details
Written by: Stanko Milosev
Category: WPF
Published: 18 June 2014
Last Updated: 18 June 2014
Hits: 7514

One more article in my list of articles about tree view expanding.

This time I will use story board from code behind.

XAML:

<Window x:Class="TreeViewAutoExpand.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:viewModel="clr-namespace:TreeViewAutoExpand.ViewModel"
        xmlns:model="clr-namespace:TreeViewAutoExpand.Model"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
            <viewModel:TreeViewAutoExpandViewModel x:Key="TreeViewAutoExpandViewModel" />
        </Grid.Resources>
        <TreeView x:Name="MyTreeView" DataContext="{StaticResource TreeViewAutoExpandViewModel}" ItemsSource="{Binding TreeViewAutoExpandItems}" Loaded="MyTreeView_Loaded">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Children}" DataType="{x:Type model:TreeViewAutoExpandModel}">
                    <TreeViewItem x:Name="myTreeViewItem" Header="{Binding Name}"/>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
            
        </TreeView>
    </Grid>
</Window>

Code:

using System.Windows;

namespace TreeViewAutoExpand
{
  using System;
  using System.Windows.Controls;
  using System.Windows.Media.Animation;

  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();
    }

    private void MyTreeView_Loaded(object sender, RoutedEventArgs e)
    {
      BooleanAnimationUsingKeyFrames bukf = new BooleanAnimationUsingKeyFrames();
      Storyboard.SetTargetProperty(bukf, new PropertyPath(TreeViewItem.IsExpandedProperty));

      Storyboard.SetTarget(bukf, MyTreeView);

      foreach (object item in MyTreeView.Items)
      {
        TreeViewItem currentContainer = (TreeViewItem)MyTreeView.ItemContainerGenerator.ContainerFromItem(item);
        if (currentContainer != null)
        {
          Storyboard.SetTarget(bukf, currentContainer);
        }
      }

      bukf.KeyFrames.Add(new DiscreteBooleanKeyFrame(false, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0.0))));
      bukf.KeyFrames.Add(new DiscreteBooleanKeyFrame(true, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(2.0))));

      Storyboard myStoryboard = new Storyboard();
      myStoryboard.Children.Add(bukf);
      myStoryboard.Begin();

    }
  }
}

Point of interest:

foreach (object item in MyTreeView.Items)
{
	TreeViewItem currentContainer = (TreeViewItem)MyTreeView.ItemContainerGenerator.ContainerFromItem(item);
	if (currentContainer != null)
	{
		Storyboard.SetTarget(bukf, currentContainer);
	}
}

This code I needed since I didn't know how to access tree view items from code behind.

Example download from here.

Expand tree view items in code behind

Details
Written by: Stanko Milosev
Category: WPF
Published: 17 June 2014
Last Updated: 30 November -0001
Hits: 5598

XAML:

<Window x:Class="TreeViewAutoExpand.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:viewModel="clr-namespace:TreeViewAutoExpand.ViewModel"
        xmlns:model="clr-namespace:TreeViewAutoExpand.Model"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
            <viewModel:TreeViewAutoExpandViewModel x:Key="TreeViewAutoExpandViewModel" />
        </Grid.Resources>
        <TreeView x:Name="MyTreeView" DataContext="{StaticResource TreeViewAutoExpandViewModel}" ItemsSource="{Binding TreeViewAutoExpandItems}" Loaded="MyTreeView_Loaded">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Children}" DataType="{x:Type model:TreeViewAutoExpandModel}">
                    <TreeViewItem x:Name="myTreeViewItem" Header="{Binding Name}"/>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
            
        </TreeView>
    </Grid>
</Window>

Thing to notice:

<TreeView x:Name="MyTreeView" DataContext="{StaticResource TreeViewAutoExpandViewModel}" ItemsSource="{Binding TreeViewAutoExpandItems}" Loaded="MyTreeView_Loaded">

Code:

using System.Windows;

namespace TreeViewAutoExpand
{
  using System.Windows.Controls;

  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();
    }

    private void ShowSelectedThing(ItemsControl parentContainer)
    {
      // check current level of tree
      foreach (object item in parentContainer.Items)
      {
        TreeViewItem currentContainer = (TreeViewItem)parentContainer.ItemContainerGenerator.ContainerFromItem(item);
        if (currentContainer != null)
        {
          currentContainer.IsExpanded = true;
        }
      }
    }

    private void MyTreeView_Loaded(object sender, RoutedEventArgs e)
    {
      ShowSelectedThing(MyTreeView);
    }

  }
}

Example you can download from here.

Expand tree when mouse is over using property triggers

Details
Written by: Stanko Milosev
Category: WPF
Published: 16 June 2014
Last Updated: 16 June 2014
Hits: 6645

To write this article I was using this answer.

XAML:

<Window x:Class="TreeViewMouseOverExpand.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:viewModel="clr-namespace:TreeViewMouseOverExpand.ViewModel"
        xmlns:treeViewModel="clr-namespace:TreeViewMouseOverExpand.Model"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
            <viewModel:TreeViewMouseOverExpandViewModel x:Key="TreeViewMouseOverExpandViewModel" />
        </Grid.Resources>
        
        <TreeView DataContext="{StaticResource TreeViewMouseOverExpandViewModel}" ItemsSource="{Binding TreeViewMouseOverExpandItems}">
            <TreeView.ItemTemplate>

                <HierarchicalDataTemplate ItemsSource="{Binding Children}" DataType="{x:Type treeViewModel:TreeViewMouseOverExpandModel}">
                    <TreeViewItem Header="{Binding Name}"/>
                </HierarchicalDataTemplate>

            </TreeView.ItemTemplate>

            <TreeView.ItemContainerStyle>
                <Style TargetType="{x:Type TreeViewItem}">
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="IsExpanded" Value="True" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </TreeView.ItemContainerStyle>

        </TreeView>
    </Grid>
</Window>

Thing which you have to notice is:

<TreeView.ItemContainerStyle>
	<Style TargetType="{x:Type TreeViewItem}">
		<Style.Triggers>
			<Trigger Property="IsMouseOver" Value="True">
				<Setter Property="IsExpanded" Value="True" />
			</Trigger>
		</Style.Triggers>
	</Style>
</TreeView.ItemContainerStyle>

Here you can download example.

  1. Expand tree when mouse is over using event triggers
  2. WPF TreeView in details
  3. BooleanToVisibilityConverter
  4. Merging of knowledge

Subcategories

C#

Azure

ASP.NET

JavaScript

Software Development Philosophy

MS SQL

IBM WebSphere MQ

MySQL

Joomla

Delphi

PHP

Windows

Life

Lazarus

Downloads

Android

CSS

Chrome

HTML

Linux

Eclipse

Page 23 of 168

  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27