我有一些具有各种集合属性的对象,我在ListBox中使用DataTemplate为ItemTemplate显示。在该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>
答案 0 :(得分:0)
我在窗口资源中定义了一个ScrollViewer样式,它定义了窗口中所有ScrollViewers的最小高度和宽度,我没有意识到ListBoxItems在内部使用ScrollViewers。