WPF MaterailDesign TreeView节点默认全部展开
程序员文章站
2022-06-08 14:00:27
...
加载TreeView后默认节点全部折叠;
<TreeView x:Name="treeView" Grid.Row="0" Foreground="White" ItemsSource="{Binding NavMenus}" MinWidth="220">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type domain:NavMenuModel}" ItemsSource="{Binding ChildMenus}">
<TextBlock Text="{Binding MenuName}" Margin="3 2" />
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type domain:MenuModel}">
<TextBlock Text="{Binding MenuName}" Margin="3 2"/>
</DataTemplate>
</TreeView.Resources>
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectedItemChanged">
<i:InvokeCommandAction Command="{Binding SelectItemChangeCommand}" CommandParameter="{Binding ElementName=treeView,Path=SelectedItem}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TreeView>
如果写上
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"/>
<Setter Property="IsSelected" Value="{Binding IsSelected,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"/>
</Style>
</TreeView.ItemContainerStyle>
则MaterialDesign 的样式失效了,如何解决这个问题呢?
想到了样式的“继承”:
<UserControl.Resources>
<Style TargetType="TreeViewItem" BasedOn="{StaticResource MaterialDesignTreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"/>
<Setter Property="IsSelected" Value="{Binding IsSelected,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"/>
</Style>
</UserControl.Resources>