如何在关注ListBox时设置所有ListBox项的背景

时间:2017-03-17 12:44:19

标签: wpf xaml listbox

我似乎无法弄明白这一点! 我想在ListBox有焦点时将所有项目的背景更改为黄色 我做错了什么?

<ListBox x:Name="ManufacturerList" Grid.Row="10" Foreground="Black" FontSize="16" Background="DarkGray" HorizontalContentAlignment="Stretch"
         ItemsSource="{Binding Manufacturers}">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="Background" Value="White" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=ManufacturerList, Path=IsFocused}" Value="True">
                    <Setter Property="Background" Value="Yellow" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

1 个答案:

答案 0 :(得分:1)

使用此代码:

<ListBox x:Name="ManufacturerList" Grid.Row="10" Foreground="Black" FontSize="16" Background="DarkGray" HorizontalContentAlignment="Stretch"
     ItemsSource="{Binding Manufacturers}">
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">                 
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="FontWeight" Value="Bold" />
                        <Setter Property="Background" Value="Transparent" />
                        <Setter Property="Foreground" Value="Black" />
                    </Trigger>
                </Style.Triggers>
                <Style.Resources>
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Yellow"/>

                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Yellow"/>

                    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Yellow" />
                </Style.Resources>
            </Style>
        </ListBox.ItemContainerStyle>
        <ListBoxItem Tag="2" Content="One" IsSelected="True" />
        <ListBoxItem Tag="5" Content="Two" />
    </ListBox>