Quantcast
Channel: Problem with TreeView
Viewing all articles
Browse latest Browse all 5

Problem with TreeView

$
0
0

Instead of defining an ItemsTemplate for the TreeView or the HierarchicalDataTemplate you could add one or two DataTemplates to the <Resources> section of the TreeView itself. Replace the x:Key with a DataType. Please refer to the following thread for an example: https://social.msdn.microsoft.com/Forums/vstudio/en-US/fdebbcf9-6e3d-4be6-b110-728154305d1c/wpf-treeview-get-child-and-parent?forum=wpf

I don't know how your Part class or whatever you call it is defined since you haven't posted the code for it but if you want to display a different template for leaf nodes, you could use two different types for parent nodes and leaf nodes (ParentPart and LeafType in the sample markup below):

<TreeView x:Name="tv" xmlns:local="clr-namespace:WpfApplication1"><TreeView.Resources><HierarchicalDataTemplate DataType="{x:Type local:ParentPart}" ItemsSource="{Binding CurrentPartsList}"><StackPanel Orientation="Horizontal"><TextBox Text="{Binding Path=VendorName}"/><TextBox Text="{Binding Path=VendorPartNo}"></TextBox></StackPanel></HierarchicalDataTemplate><DataTemplate DataType="{x:Type local:LeafType}"><StackPanel Orientation="Vertical"><TextBox Text="{Binding Path = VendorPartNoLink}" /><TextBox Text="{Binding Path = AlternativeLink1}" /><TextBox Text="{Binding Path = AlternativeLink2}" /><TextBox Text="{Binding Path = AlternativeLink3}" /></StackPanel></DataTemplate></TreeView.Resources></TreeView> 

If you are using the same type for parent and leaf nodes, you will have to modify the template based on the state of the node. You could for example do this using DataTriggers. Add an "IsParent" property or something to your class and then show/hide controls in the template based on the value of this one. Something like this:

<HierarchicalDataTemplate DataType="{x:Type local:ParentPart}" ItemsSource="{Binding CurrentPartsList}"><StackPanel x:Name="sp" Orientation="Horizontal"><TextBox x:Name="a" Text="{Binding Path=VendorName}"/><TextBox x:Name="b" Text="{Binding Path=VendorPartNo}"></TextBox><TextBox x:Name="c" Text="{Binding Path = VendorPartNoLink}" /><!-- +...--></StackPanel><HierarchicalDataTemplate.Triggers><!-- Add an IsParent property to your node class --><DataTrigger Binding="{Binding IsParent}" Value="False"><Setter TargetName="sp" Property="Orientation" Value="Vertical"/><Setter TargetName="a" Property="Visibility" Value="Collapsed"/><Setter TargetName="b" Property="Visibility" Value="Collapsed"/><Setter TargetName="c" Property="Visibility" Value="Visible"/></DataTrigger></HierarchicalDataTemplate.Triggers></HierarchicalDataTemplate>

Hope that helps.

Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.


Viewing all articles
Browse latest Browse all 5

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>