Silverlight - TreeView项目上方/下方的空间

时间:2022-11-17 06:09:22

I'm trying to reduce the amount of space between TreeView items to fit more vertically. I'm guessing that it's a matter of styling the ItemContainer Style or ItemContainer template in the TreeView, but I can't seem to get at the correct properties. Can anyone point me in the right direction?

我正在尝试减少TreeView项目之间的空间量以更加垂直。我猜这是在TreeView中设置ItemContainer Style或ItemContainer模板的问题,但我似乎无法获得正确的属性。谁能指出我正确的方向?

1 个解决方案

#1


You are correct, you need to alter the ItemContainerStyle of the TreeView control.

你是对的,你需要改变TreeView控件的ItemContainerStyle。

All containers within the default TreeViewItem style's control template are set to "auto" and stretch to consume as much vertical space that is needed by the item content.

默认TreeViewItem样式的控件模板中的所有容器都设置为“auto”并拉伸以消耗项目内容所需的垂直空间。

You can force this by doing either of two things:

您可以通过以下两种方法之一强制执行此操作:

  1. Reduce the size of the content of each item by altering the TreeView's ItemTemplate
  2. 通过更改TreeView的ItemTemplate来减小每个项目内容的大小

  3. Invert the Margin on the Grid inside of the TreeViewItem's default control template.
  4. 在TreeViewItem的默认控件模板内反转网格上的边距。

Below is an excerpt from a new control template that I created for the TreeViewItem. Notice how I set the Margin to be "0,-4,0,-4". This tells the content to take 4 less pixels on top and bottom of the item, thus reducing the vertical real estate of each item.

下面是我为TreeViewItem创建的新控件模板的摘录。注意我如何将Margin设置为“0,-4,0,-4”。这告诉内容在项目的顶部和底部减少4个像素,从而减少每个项目的垂直空间。

<ControlTemplate TargetType="controls:TreeViewItem">
    <Grid Background="{x:Null}" Margin="0,-4,0,-4">
        ...

#1


You are correct, you need to alter the ItemContainerStyle of the TreeView control.

你是对的,你需要改变TreeView控件的ItemContainerStyle。

All containers within the default TreeViewItem style's control template are set to "auto" and stretch to consume as much vertical space that is needed by the item content.

默认TreeViewItem样式的控件模板中的所有容器都设置为“auto”并拉伸以消耗项目内容所需的垂直空间。

You can force this by doing either of two things:

您可以通过以下两种方法之一强制执行此操作:

  1. Reduce the size of the content of each item by altering the TreeView's ItemTemplate
  2. 通过更改TreeView的ItemTemplate来减小每个项目内容的大小

  3. Invert the Margin on the Grid inside of the TreeViewItem's default control template.
  4. 在TreeViewItem的默认控件模板内反转网格上的边距。

Below is an excerpt from a new control template that I created for the TreeViewItem. Notice how I set the Margin to be "0,-4,0,-4". This tells the content to take 4 less pixels on top and bottom of the item, thus reducing the vertical real estate of each item.

下面是我为TreeViewItem创建的新控件模板的摘录。注意我如何将Margin设置为“0,-4,0,-4”。这告诉内容在项目的顶部和底部减少4个像素,从而减少每个项目的垂直空间。

<ControlTemplate TargetType="controls:TreeViewItem">
    <Grid Background="{x:Null}" Margin="0,-4,0,-4">
        ...