You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在不修改ControlTemplate的情况下更改禁用状态ListView的背景色?

如何在不修改ControlTemplate的情况下更改禁用状态ListView的背景色

我希望在不修改ControlTemplate的前提下,更改禁用状态下ListView的背景色,为此查阅了相关官方文档。

以下是我的XAML代码:

<Window.Resources>
    <Style TargetType="{x:Type ListView}">
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Visible"/>
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Visible"/>
        <Style.Triggers>
            <Trigger Property="IsEnabled" Value="True">
                <!--#region Working -->
                <Setter Property="Background" Value="BlueViolet"/>
                <Setter Property="Foreground" Value="Blue"/>
                <Setter Property="FontWeight" Value="Bold"/>
                <Setter Property="FontSize" Value="40"/>
                <!--#endregion-->
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <!--#region Not Working -->
                <Setter Property="Background" Value="Green"/>
                <!--<Setter Property="Background" Value="Green" TargetName="PART_ScrollContentPresenter"/>-->
                <!--#endregion-->
                
                <!--#region Working -->
                <Setter Property="FontWeight" Value="Bold"/>
                <Setter Property="FontSize" Value="40"/>
                <!--#endregion-->
            </Trigger>
        </Style.Triggers>
        <Style.Resources>
            <!--  ScrollViewer Right Bottom Corner Color  -->
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="LawnGreen"/>

            <!--  Disabled Text Color  -->
            <SolidColorBrush x:Key="{x:Static SystemColors.GrayTextBrushKey}" Color="Red"/>
        </Style.Resources>
    </Style>
</Window.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <ListView Grid.Column="0" Margin="10" SelectedIndex="1">
        <ListViewItem Content="1" Height="100"/>
        <ListViewItem Content="2" Height="100"/>
        <ListViewItem Content="3" Height="100"/>
        <ListViewItem Content="4" Height="100"/>
        <ListViewItem Content="5" Height="100"/>
    </ListView>
    <ListView Grid.Column="1" Margin="10" SelectedIndex="1" IsEnabled="False">
        <ListViewItem Content="1" Height="100"/>
        <ListViewItem Content="2" Height="100"/>
        <ListViewItem Content="3" Height="100"/>
        <ListViewItem Content="4" Height="100"/>
        <ListViewItem Content="5" Height="100"/>
    </ListView>
</Grid>

第一次尝试

我在文档中找到"PART_ScrollContentPresenter"元素,尝试修改其背景色,但无法找到对应的TargetName:

<Setter Property="Background" Value="Green" TargetName="PART_ScrollContentPresenter"/>

第二次尝试

直接设置Background属性无效:

<Setter Property="Background" Value="Green"/>

最后一次尝试

我尝试修改Style.Resources,成功更改了禁用状态ListView的文本颜色,但遍历SystemColors类所有键后,背景色仍未改变。

请问有没有办法在不修改ControlTemplate的情况下更改背景色?


编辑补充

以下方法能更改背景色,但仍有细白边框无法消除:

<Style.Resources>
    <Style TargetType="ScrollViewer">
        <Style.Triggers>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Background" Value="Red"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Style.Resources>

内容的提问来源于stack exchange,提问作者jhKim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 03:47:05