Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I am using WPF treeview. My Items are in Hierarchical data template.
I want to catch the extended item event, using MVVM, and send to the function on view model the treeview that was extended on the object it represents.

Thanks ahead,

Here is my code:

XML
<TreeView  Name="ScenariosTreeView" ItemsSource="{Binding Path=Cat, Mode=TwoWay}" Focusable="True" >

               <TreeView.InputBindings>
                   <KeyBinding Key="Delete" Command="{Binding DeleteCommand}" CommandParameter="{Binding SelectedValue ,ElementName=ScenariosTreeView}" />
               </TreeView.InputBindings>


               <TreeView.ItemContainerStyle>
                   <Style TargetType="{x:Type TreeViewItem}">
                       <Setter Property="IsExpanded" Value="{Binding IsExtended, Mode=TwoWay}" />
                       <Setter Property="IsSelected" Value="{Binding IsExtended, Mode=TwoWay}"/>
                   </Style>
               </TreeView.ItemContainerStyle>

               <TreeView.Resources>

                       <HierarchicalDataTemplate DataType="{x:Type sotc:ScenarioCategory}" ItemsSource="{Binding Path=ScenarioList}" >
                       <TextBlock  Text="{Binding Path=Name}" Foreground="Black" IsEnabled="True" Focusable="True"/>
                      </HierarchicalDataTemplate>


                   <DataTemplate DataType="{x:Type sotc:Scenario}" >
                       <TextBlock  Text="{Binding Path=Name, Mode=TwoWay}" />
                   </DataTemplate>

               </TreeView.Resources>

           </TreeView>



The IsExpanded event does not fires up, any ideas?
Posted
Updated 26-Jan-16 20:27pm
v2
Comments
Sergey Alexandrovich Kryukov 25-Jan-16 10:24am    
Do you mean "expanded", the event invoked when one expands the tree node?
—SA

Please see my comment to the question. If you mean the event when a node is expanded, this is the event of TreeViewItem:
TreeViewItem.Expanded Event (System.Windows.Controls)[^].

The code sample shows the case when an event handler is "manually" added to the invocation list of the event instance, in XAML. It's possible that you have to, for example, eventually traverse the tree and add the event handler to each node.

—SA
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Jan-16 11:31am    
No, it is not. This is a WPF event. Please read carefully.
Pay attention for the line:
Assembly: PresentationFramework (in PresentationFramework.dll)
—SA
Richard Deeming 25-Jan-16 11:34am    
D'Oh! I read "Forms" instead of "Controls". Comment withdrawn.
No need to mess around with the control's events - just bind the IsExpanded property to a property on your view-model, and have the view-model do whatever you need it to when the property changes.

Have a look at this article for a more in-depth look at using the TreeView control with MVVM:
Simplifying the WPF TreeView by Using the ViewModel Pattern[^]
 
Share this answer
 
Comments
Jen123sml 26-Jan-16 6:18am    
That is a good way. However, I didn't use special objects just to display them in the tree. I am looking for another way to get the IsExpanded event of the treeviewItem, when I am using Hierarchical data template. maybe trigger or something like that
If you just want them to expand or collapse and that is all you are looking for and you have them in the ObservableCollection, then the onchanged event will only fire on the Add(), Clear(), and Remove() events, so try a templist to hold your itemsource

C#
foreach(type item in Cat)
{
item.IsExpanded = true;
templist.add(item);
}
Cat.Clear();
foreach(type item in templist)
{
Cat.Add(item);
}


one of those funny issues with observable collections
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900