- Details
- Written by: Stanko Milosev
- Category: WPF
- Hits: 6190
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>
- Details
- Written by: Stanko Milosev
- Category: WPF
- Hits: 6469
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.
- Details
- Written by: Stanko Milosev
- Category: WPF
- Hits: 4747
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.
- Details
- Written by: Stanko Milosev
- Category: WPF
- Hits: 5595
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.