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
  3. C#

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: 7469

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: 5563

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: 6574

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.

Expand tree when mouse is over using event triggers

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

Now idea is to expand tree items after some period of time.

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.ItemContainerStyle>
                <Style TargetType="TreeViewItem">
                    <Style.Triggers>
                        <EventTrigger RoutedEvent="MouseEnter">
                            <EventTrigger.Actions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <BooleanAnimationUsingKeyFrames 
                                            Duration="00:00:01" 
                                            Storyboard.TargetProperty="(TreeViewItem.IsExpanded)">
                                            <DiscreteBooleanKeyFrame Value="True" />
                                        </BooleanAnimationUsingKeyFrames>
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger.Actions>
                        </EventTrigger>
                    </Style.Triggers>
                </Style>
            </TreeView.ItemContainerStyle>            
            
            <TreeView.ItemTemplate>

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

                    </TreeViewItem>

                </HierarchicalDataTemplate>

            </TreeView.ItemTemplate>

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

Example download from here.

---

Problem with above example is that after executing event trigger you will not be able to change any property (that means that expand / collapse will not work anymore). Solution is in FillBehavior="Stop" (see How to: Set a Property After Animating It with a Storyboard), but, I wouldn't write this article if there is no at least one "but" :), I didn't find proper solution, until now, problem is that if you FillBehavior="Stop" in MouseEnter then trigger will not work at all... if you implement MouseLeave trigger with FillBehavior="Stop", then it will partly work with strange behavior...

In my case, for our product I implemented TreeViewItem.PreviewDragEnter (without FillBehavior="Stop"), and then TreeViewItem.MouseLeave with FillBehavior="Stop", so I have two different triggers on which I could respond... but for MouseEnter / MouseLeave I don't have clear idea how to do it without some hacking in code behind...

  1. WPF TreeView in details
  2. BooleanToVisibilityConverter
  3. Merging of knowledge
  4. Datatemplates in user control

Subcategories

WPF

Beginning

Code snippets

NUnit

LINQ

Windows Forms

Page 23 of 39

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