WPF TreeView 在根根折叠时不会折叠其以前使用的空间
我有一些具有各种集合属性的对象,我使用 ItemTemplate 的 DataTemplate 在 ListBox 中显示这些对象。在该 DataTemplate 中,有一个 TreeView,它将各种属性显示为根 TreeViewItem 的 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>
I have some objects with various collection properties that I'm displaying in a ListBox using a DataTemplate for the ItemTemplate. Within that DataTemplate is a TreeView which displays various properties as TreeViewItem children of a root TreeViewItem which represents the items being displayed in the ListBoxItem. My problem is this : the ListBoxItems have the vertical size of the fully expanded TreeView within the DataTemplate. Is there any way to change this so that the ListBoxItems will have the minimum size possible ?
My 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在窗口资源中定义了 ScrollViewer 的样式,该样式定义了窗口中所有 ScrollViewers 的最小高度和宽度,但我没有意识到 ListBoxItems 在内部使用 ScrollViewers。
I had a style for ScrollViewer defined in the window resources that was defining the minimum height and width for all ScrollViewers in the window, and I didn't realize that ListBoxItems use ScrollViewers internally.