在运行时更改 ListBox 项目样式

发布于 2024-08-07 18:07:44 字数 324 浏览 3 评论 0原文

我有一个列表框,其中的项目使用 ResourceDictionary 样式进行样式设置,然后将其附加到 ItemContainerStyle 属性。这使我的 ListBoxItems 的 BorderThickness 为 1。

现在我想单独折叠项目,因此我使用 Visibility.Collapsed,但由于某种原因,ItemContainerStyle 创建的边框不会随列表框项目的其余部分一起消失。就好像它在我的项目后面创建了一个层,尽管项目被折叠,但它仍然存在。

如何在运行时将 ListBoxItem(或此额外层)的 BorderThickness 设置为 0?

问候 斯克

I have a listbox where the items are styled using a ResourceDictionary style which is then attached to the ItemContainerStyle property. This gives my ListBoxItems a BorderThickness of let's say 1.

Now I want to collapse the items individually, so I use Visibility.Collapsed but for some reason the border that the ItemContainerStyle has created does not vanish with the rest of the list box item. It is as if it has created a layer behind my item and this remains despite the item being collapsed.

How do I set the ListBoxItem's (or this extra layer's) BorderThickness to 0 at run-time?

Regards
sk

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

腻橙味 2024-08-14 18:07:44

尝试使用自定义触发器:

    <Style TargetType="{x:Type ListBoxItem}">
        <Style.Triggers>
            <Trigger Property="Visibility" Value="Collapsed">
                <Setter Property="BorderThickness" Value="0,0,0,0"/>
            </Trigger>
            <Trigger Property="Visibility" Value="Visible">
                <Setter Property="BorderThickness" Value="0,0,0,1"/>
            </Trigger>
        </Style.Triggers>
    </Style>

显然改变你的边框厚度值,但这应该可以解决问题(或非常接近于此的东西)

try using a custom triggers:

    <Style TargetType="{x:Type ListBoxItem}">
        <Style.Triggers>
            <Trigger Property="Visibility" Value="Collapsed">
                <Setter Property="BorderThickness" Value="0,0,0,0"/>
            </Trigger>
            <Trigger Property="Visibility" Value="Visible">
                <Setter Property="BorderThickness" Value="0,0,0,1"/>
            </Trigger>
        </Style.Triggers>
    </Style>

Obviously change your border thickness values, but this should do the trick (or something very close to this)

梦纸 2024-08-14 18:07:44

我尝试重现该问题,但发现边框确实按预期折叠:

<StackPanel>
  <StackPanel.Resources>
    <BooleanToVisibilityConverter x:Key="BooleanToVisibility" />
    <Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
      <Setter Property="BorderBrush" Value="Black" />
      <Setter Property="BorderThickness" Value="1" />
    </Style>
  </StackPanel.Resources>

  <CheckBox x:Name="_show"
            Content="Show Item 2"
            IsChecked="True" />

  <ListBox ItemContainerStyle="{StaticResource ListBoxItemStyle}">
    <ListBoxItem Content="Item 1" />
    <ListBoxItem Content="Item 2">
      <ListBoxItem.Visibility>
        <Binding ElementName="_show"
                 Path="IsChecked"
                 Converter="{StaticResource BooleanToVisibility}" />
      </ListBoxItem.Visibility>
    </ListBoxItem>
    <ListBoxItem Content="Item 3" />
  </ListBox>
</StackPanel>

您确定 ListBoxItem 是正在折叠的对象(而不是 ListBoxItem 中的 UI 对象)吗?

I tried to reproduce the issue, but found that the border does collapse as expected:

<StackPanel>
  <StackPanel.Resources>
    <BooleanToVisibilityConverter x:Key="BooleanToVisibility" />
    <Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
      <Setter Property="BorderBrush" Value="Black" />
      <Setter Property="BorderThickness" Value="1" />
    </Style>
  </StackPanel.Resources>

  <CheckBox x:Name="_show"
            Content="Show Item 2"
            IsChecked="True" />

  <ListBox ItemContainerStyle="{StaticResource ListBoxItemStyle}">
    <ListBoxItem Content="Item 1" />
    <ListBoxItem Content="Item 2">
      <ListBoxItem.Visibility>
        <Binding ElementName="_show"
                 Path="IsChecked"
                 Converter="{StaticResource BooleanToVisibility}" />
      </ListBoxItem.Visibility>
    </ListBoxItem>
    <ListBoxItem Content="Item 3" />
  </ListBox>
</StackPanel>

Are you certain the ListBoxItem is the object being collapsed (as opposed to the UI object in the ListBoxItem)?

把梦留给海 2024-08-14 18:07:44
foreach(ListBoxItem item in listBox1.Items){
   item.BorderThickness = new Thickness(0);
}

这就是答案,但我不推荐,因为您无法撤消样式以恢复原始内容,而是您应该根据某些状态选择一些不同的数据绑定方法。

foreach(ListBoxItem item in listBox1.Items){
   item.BorderThickness = new Thickness(0);
}

This is the answer but I wouldnt recommend because you cant undo the style to bring back what was original instead you should choose some different approach with databinding on the basis of certain states.

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