WPF Treeview não colapsando seu espaço anteriormente usado no colapso da raiz raiz
-
18-09-2019 - |
Pergunta
Eu tenho alguns objetos com várias propriedades de coleta que estou exibindo em uma caixa de listagem usando um datatemplate para o itemTemplate. Dentro desse Datatemplate há uma View Tree que exibe várias propriedades como TreeViewItem Children of a Root TreeViewItem, que representa os itens que estão sendo exibidos no ListBoxItem. Meu problema é o seguinte: os ListBoxItems têm o tamanho vertical do TreeView totalmente expandido no Datatemplate. Existe alguma maneira de mudar isso para que os ListBoxItems tenham o tamanho mínimo possível?
Meu xaml:
<ListBox x:Name="ShackList" ItemsSource="{Binding}" ToolTip="Right click for options" SelectionMode="Single">
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type model:ShackConfigurationRepository}">
<TreeView BorderThickness="0" Margin="0">
<TreeView.Resources>
<Style x:Key="deviceRepoNodeStyle" TargetType="{x:Type TreeViewItem}">
<Setter Property="Margin" Value="3"/>
<Setter Property="ContextMenu" Value="{StaticResource deviceTypeTreeViewItemContextMenu}"/>
</Style>
</TreeView.Resources>
<TreeViewItem>
<TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<Image Source="Resources/Shack_64x64_72.png" Height="24" Width="24"/>
<TextBlock VerticalAlignment="Center" Margin="5,0,0,0" Text="{Binding Path=ShackName}" />
</StackPanel>
</TreeViewItem.Header>
<!--
The 'Tag' attribute of the following TreeViewItems is a value of
DeviceType used to control which collection gets set as the content
of the device list.
-->
<TreeViewItem Header="Pendants" Tag="PENDANT" Selected="ShackDevice_Selected" DataContext="{Binding}" Style="{StaticResource deviceRepoNodeStyle}" PreviewMouseRightButtonDown="DeviceTreeNode_PreviewMouseRightButtonDown"/>
<TreeViewItem Header="Oscillators" Tag="OSCILLATOR" Selected="ShackDevice_Selected" DataContext="{Binding}" Style="{StaticResource deviceRepoNodeStyle}" PreviewMouseRightButtonDown="DeviceTreeNode_PreviewMouseRightButtonDown"/>
<TreeViewItem Header="Thru-Arc-Tracking" Tag="TAT" Selected="ShackDevice_Selected" DataContext="{Binding}" Style="{StaticResource deviceRepoNodeStyle}" PreviewMouseRightButtonDown="DeviceTreeNode_PreviewMouseRightButtonDown"/>
<TreeViewItem Header="Travel" Tag="TRAVEL" Selected="ShackDevice_Selected" DataContext="{Binding}" Style="{StaticResource deviceRepoNodeStyle}" PreviewMouseRightButtonDown="DeviceTreeNode_PreviewMouseRightButtonDown"/>
</TreeViewItem>
</TreeView>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Solução
Eu tinha um estilo para o ScrollViewer definido nos recursos da janela que estava definindo a altura e a largura mínimas para todos os scrollViewers na janela, e não percebi que o ListBoxItems usava o ScrollViewers internamente.