如何在列表框中更改所选项“text”的前景颜色

时间:2022-11-20 23:10:28

I have a ListBox with a DataTemplate. I'm trying to change the color of the text/foreground to white for the selected item in the listbox. I have seriously tried like 30 different ways to do it that I've found on google. I just can't get it to work. How can I change the foreground color?

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

Also, I would like to point out that I would like to not rely on the method that uses the SystemColors because I read that it won't work in .net 4.5 so I don't want it to break when we move our application to 4.5 some day. Here is my listbox xaml:

另外,我想指出的是,我不希望依赖使用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>

1 个解决方案

#1


8  

What about this:

这个:

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

In a DataTemplate that uses TextBlocks, the Foreground property would simply be inherited:

在使用textblock的DataTemplate中,前台属性将简单地继承:

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

In your DataTemplate with Label controls Foreground value inheritance does not work (see here why). But you may always bind to the Foreground property of the ListBoxItem like this:

在使用标签控件的DataTemplate中,前台值继承不起作用(请参见这里的原因)。但是,您可以始终绑定到ListBoxItem的前台属性如下:

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

Or you may simply replace your Labels with TextBlocks.

或者你可以用文本块替换你的标签。

#1


8  

What about this:

这个:

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

In a DataTemplate that uses TextBlocks, the Foreground property would simply be inherited:

在使用textblock的DataTemplate中,前台属性将简单地继承:

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

In your DataTemplate with Label controls Foreground value inheritance does not work (see here why). But you may always bind to the Foreground property of the ListBoxItem like this:

在使用标签控件的DataTemplate中,前台值继承不起作用(请参见这里的原因)。但是,您可以始终绑定到ListBoxItem的前台属性如下:

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

Or you may simply replace your Labels with TextBlocks.

或者你可以用文本块替换你的标签。