【问题标题】:How to add the spacing between the items in ListBox Wpf如何在 ListBox Wpf 中的项目之间添加间距
【发布时间】:2016-04-01 07:19:54
【问题描述】:

我有一个包含 ListBoxItems 的 ListBox,我已将按钮添加为垂直形式的 ListBoxItems。 按钮之间的间距由边距设置。 但是当我单击按钮下方时,它正在调用该事件。

我应该如何删除它?以及如何将 ListBoxItem & Margin 的高度设置为该项目?

【问题讨论】:

  • “点击按钮下方”是什么意思?你的意思是Margin 区域。另外,分享您尝试过的代码。

标签: c# wpf listbox


【解决方案1】:

您可以使用ListBox.ItemContainerStyle 获取或设置渲染项目容器时使用的样式(ListBoxItems),Margin 将为您完成所需的任务:尝试以下代码:

<ListBox.ItemContainerStyle>
      <Style TargetType="ListBoxItem">
            <Setter Property="Margin" Value="5"/>
            <Setter Property="Height" Value="75"/>    <!-- For setting height of control-->
            <Setter Property="Width" Value="75"/>    <!-- For setting widthof control-->
      </Style>
</ListBox.ItemContainerStyle>

【讨论】:

  • 我之前尝试过设置,但是当点击按钮的向下区域时仍然可以点击
  • 我不想让那个部分可以点击,也尝试设置 ListBoxItem & Padding 的边距,
  • &lt;Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem"&gt; &lt;Setter Property="Margin" Value="25"/&gt; &lt;Setter Property="Height" Value="75"/&gt; &lt;Setter Property="Width" Value="75"/&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt;