【问题标题】:How do I change the selected item 'text' foreground color in a listbox如何更改列表框中所选项目“文本”的前景色
【发布时间】:2013-02-10 22:15:39
【问题描述】:

我有一个带有 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>

【问题讨论】:

    标签: c# wpf xaml wpf-controls


    【解决方案1】:

    这个呢:

    <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 值继承不起作用(请参阅here 为什么)。但是你可能总是像这样绑定到 ListBoxItem 的 Foreground 属性:

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

    或者你可以简单地用 TextBlocks 替换你的标签。

    【讨论】:

    • 这实际上是我尝试过的事情之一。我继续尝试再次尝试,但它不起作用。也许它与我使用数据模板有关?
    • 谢谢。这似乎奏效了。但它会变得混乱,也许你可以解决这两个问题。一个是当列表框失去控制但仍然有一个“选择”的列表框项时,我如何让颜色翻转回正常/黑色?另外,我的列表框中有一些标签,我怎样才能轻松地将它们全部绑定到选定的前景色而不显式地将其添加到每个标签?这就是会变得混乱的地方
    • 嗯,样式触发器位于IsSelected 属性上。也许您可以编写一个MultiTrigger,同时将IsFocused 属性考虑在内。而且我认为当您将标签替换为 TextBlocks 时,它根本不会变得混乱。为什么你需要标签?
    • 好的,我试试多触发器。但是,关于文本块的最后一句话是什么意思?
    • 我的意思是使用TextBlock 控件。您不需要(更复杂的)标签控件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-01
    • 2018-09-01
    • 2014-07-29
    • 2010-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多