禁用列表框不会改变样式中的背景颜色

发布于 2025-01-01 07:10:40 字数 608 浏览 7 评论 0原文

我有这种简单的样式,当禁用 ListBox 时,它不会更改 ListBox Background

<Style TargetType="ListBox" >                    
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="True">
            <Setter Property="Background" Value="Red"/>
        </Trigger>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Background" Value="Orange"/>
        </Trigger>
    </Style.Triggers>
</Style>

Snoop 在这方面没有帮助我,我不能想出一种无需重写模板的简单方法。有人有一个简单的方法来让它工作吗? TIA。

I have this simple style that does not change the ListBox Background when the ListBox is disabled:

<Style TargetType="ListBox" >                    
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="True">
            <Setter Property="Background" Value="Red"/>
        </Trigger>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Background" Value="Orange"/>
        </Trigger>
    </Style.Triggers>
</Style>

Snoop does not help me on this one and I cannot figure a simple way without overriding templates. Anyone have a simple way to get this working? TIA.

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

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

发布评论

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

评论(2

狼性发作 2025-01-08 07:10:40

做到这一点的唯一方法是覆盖模板

The only way to do this is by overriding the template

镜花水月 2025-01-08 07:10:40

当列表框被禁用时,您可以通过简单地更改内置模板正在使用的颜色来更改列表框本身的背景颜色。您可以通过样式资源来做到这一点。只需将以下代码粘贴到列表框元素中,当您禁用该框时,背景将是透明的。

<ListBox.Style>
    <Style TargetType="{x:Type ListBox}">
        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/>
        </Style.Resources>
    </Style>
</ListBox.Style>

当单个项目突出显示时以及当框失去焦点时想要修改其背景颜色也是很常见的。要更改这些,您可以参考这篇文章:
https://stackoverflow.com/a/7298301/1721136

You can change the background color of the listbox itself when the listbox is disabled by simply changing the colors that the built-in template is using. You can do this via style resources. Just paste the below code into your Listbox element and the background will be transparent when you disable the box.

<ListBox.Style>
    <Style TargetType="{x:Type ListBox}">
        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/>
        </Style.Resources>
    </Style>
</ListBox.Style>

It is also very common to want to modify the background color of a single item when it's highlighted and when the box loses focus.. to change these, you can reference this post:
https://stackoverflow.com/a/7298301/1721136

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