如何更改所选项目';text';列表框中的前景色

本文关键字:前景色 列表 text 项目 何更改 选项 | 更新日期: 2023-09-27 17:58:30

我有一个带有DataTemplate的ListBox。我正在尝试将列表框中所选项目的文本/前景颜色更改为白色。我认真地尝试了30种不同的方法,我在谷歌上找到了。我就是不能让它工作如何更改前景色

此外,我想指出的是,我不想依赖使用SystemColors的方法,因为我读到它在.net 4.5中不起作用,所以我不希望有一天我们将应用程序移动到4.5时它会崩溃。这是我的列表框xaml:

<ListBox Grid.Row="1" x:Name="Alerts" ItemsSource="{Binding Alerts}" HorizontalContentAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled" AlternationCount="2">
<ListBox.ItemTemplate>
    <DataTemplate>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="Auto"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Grid Grid.Column="0">
                <Grid.RowDefinitions>
                    <RowDefinition></RowDefinition>
                    <RowDefinition></RowDefinition>
                </Grid.RowDefinitions>
                <StackPanel Grid.Row="0" Orientation="Horizontal">
                    <Label Content="{Binding Type}" HorizontalAlignment="Left" Padding="1,1,1,0" />
                    <Label Content=" - " Padding="1,1,1,0"></Label>
                    <Label Content="{Binding Date}" HorizontalAlignment="Left" Padding="1,1,1,0" />
                </StackPanel>
                <Label Grid.Row="1" Content="{Binding Message}" HorizontalAlignment="Left" Margin="0" Padding="1,0,1,1" />
            </Grid>
            <Button Grid.Column="1"
                    CommandParameter="{Binding .}"
                    Command="{Binding Path=DataContext.Commands.RemoveAlertCommand, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}"
                    Content="X" HorizontalAlignment="Right" Width="Auto" Height="Auto" Foreground="#FFD40000" FontWeight="Bold" VerticalAlignment="Center" />
        </Grid>
    </DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle>
    <Style  TargetType="{x:Type ListBoxItem}">
        <Style.Triggers>
            <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                <Setter Property="Background" Value="LightGray"></Setter>
            </Trigger>
            <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                <Setter Property="Background" Value="Ivory"></Setter>
            </Trigger>
        </Style.Triggers>
    </Style>
</ListBox.ItemContainerStyle>
</ListBox>

如何更改所选项目';text';列表框中的前景色

这个怎么样:

<ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem">
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Foreground" Value="White"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</ListBox.ItemContainerStyle>

在使用TextBlocks的DataTemplate中,Foreground属性将被简单地继承:

<ListBox.ItemTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding}"/>
    </DataTemplate>
</ListBox.ItemTemplate>

在带有标签的DataTemplate控件中,Foreground值继承不起作用(请参阅此处的原因)。但您可能总是绑定到ListBoxItem的Foreground属性,如下所示:

<ListBox.ItemTemplate>
    <DataTemplate>
        <Label Content="{Binding}"
               Foreground="{Binding Foreground,
                            RelativeSource={RelativeSource Mode=FindAncestor,
                                            AncestorType=ListBoxItem}}"/>
    </DataTemplate>
</ListBox.ItemTemplate>

或者,您可以简单地将标签替换为文本块。