WPF TreeView 在根根折叠时不会折叠其以前使用的空间

发布于 2024-08-16 12:08:46 字数 2675 浏览 5 评论 0原文

我有一些具有各种集合属性的对象,我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2024-08-23 12:08:46

我在窗口资源中定义了 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文