我有一些具有各种集合属性的对象,我使用 ItemTemplate 的 DataTemplate 在 ListBox 中显示这些对象。在该 DataTemplate 中有一个 TreeView,它将各种属性显示为根 TreeViewItem 的 TreeViewItem 子项,该子项表示 ListBoxItem 中显示的项目。我的问题是:ListBoxItems 在 DataTemplate 中具有完全展开的 TreeView 的垂直大小。有什么办法可以改变它,使 ListBoxItems 的尺寸尽可能小?
我的 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>