为ListBox中的选定项设置背景颜色

pap*_*zzo 7 wpf xaml background listbox selecteditem

我无法在列表框中设置所选项目的背景颜色.我不想在这个例子中交替颜色.我把它们作为测试,他们工作.触发器IsSelected随着字体粗体变为粗体而前景变为红色而触发.将高亮颜色画笔设置为SteelBlue无法达到预期效果,因为当ListBox失去焦点时它会消失.当ListBox失去焦点并且是我想要的时候,红色和粗体确实成立.我想要为所选项目拍摄并保持背景颜色.现在,所选项目的背景为白色,并在ListBox失去焦点时保持不变.感谢您的帮助,我将测试任何建议的修复程序.

    <ListBox Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="1" Name="WFEnum" Visibility="Visible" BorderThickness="2" Margin="1" Padding="2,2,7,2"
             ItemsSource="{Binding Path=SearchItem.SrchWorkFlows}" HorizontalAlignment="Left" 
             PresentationTraceSources.TraceLevel="High" AlternationCount="2" >
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="VerticalContentAlignment" Value="Center" />
                <Style.Triggers>
                    <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                        <Setter Property="Background" Value="LightGreen"></Setter>
                    </Trigger>
                    <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                        <Setter Property="Background" Value="LightPink"></Setter>
                    </Trigger>
                    <Trigger Property="IsSelected" Value="True" >
                        <Setter Property="FontWeight" Value="Bold" />
                        <Setter Property="Background" Value="SteelBlue" />
                        <Setter Property="Foreground" Value="Red" />
                    </Trigger>
                </Style.Triggers>
                <Style.Resources>
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
                </Style.Resources>
            </Style>
        </ListBox.ItemContainerStyle>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Path=Name, Mode=OneWay}" Background="Transparent" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
Run Code Online (Sandbox Code Playgroud)

Fre*_*lad 14

您指定的一个背景的SelectedItem ListBox与SystemColors.HighlightBrushKey(聚焦)和SystemColors.ControlBrushKey(不聚焦)

<Style.Resources>
    <!-- Background of selected item when focussed -->
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
                     Color="Green"/>
    <!-- Background of selected item when not focussed -->
    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
                     Color="LightGreen" />
</Style.Resources> 
Run Code Online (Sandbox Code Playgroud)

  • 值得一提的是,截至2015年1月,这对使用标准主题的System.Windows.Controls.ListBox的选择突出显示颜色没有影响.触发器适用于前景但不适用于背景.您必须重新模板ListBoxItem. (11认同)

H.B*_*.B. 10

<ListBox.Resources>
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}">SteelBlue</SolidColorBrush>
</ListBox.Resources>
Run Code Online (Sandbox Code Playgroud)

如果您希望将其应用于焦点之外,则需要覆盖其他键:

<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}">SteelBlue</SolidColorBrush>
Run Code Online (Sandbox Code Playgroud)

  • @BalamBalam:"干净"值得商榷,我的回答快七分钟哦,好吧,谢谢... (2认同)