引言
项目经常会用Treeview来组织一些具有层级结构的数据,本节就将项目使用Treeview常见的问题作一个总结。
DataBinding数据绑定
DataTemplate自定义
<HierarchicalDataTemplate DataType="{x:Type viewModels:FieldViewModel}" ItemsSource="{Binding SubViewModels}"> <StackPanel Orientation="Horizontal"> <TextBlock Text="F:" Style="{StaticResource IconTextStyle}" Background="LightCoral"/> <TextBlock Text="{Binding CurrentEntity.Value.Name}" Focusable="True"> <TextBlock.ContextMenu> <ContextMenu> <MenuItem Header="New Well" Command="{Binding AddCommand}" CommandParameter="{Binding Path=CurrentEntity.Value}" /> <MenuItem Header="Delete" Command="{Binding DeleteCommand}" CommandParameter="{Binding Path=CurrentEntity.Value}" /> </ContextMenu> </TextBlock.ContextMenu> </TextBlock> </StackPanel> </HierarchicalDataTemplate>
Style自定义
Events顺序
SelectedItemChanged 发生在TreeView向新selectedItem聚焦(set focus on its new selected item).
一句话: selectedItemChanged –>set foucs on the new selected item.