从 WPF 中的代码隐藏更改 ListBoxItem 的前景

Changing ListBoxItem's Foreground from Code Behind in WPF

在 WPF 中,有没有一种方法可以通过隐藏代码更改特定 ListBoxItem 的前景?

我有这个列表框:

    <ListBox x:Name="songsListBox" Background="{DynamicResource Gray}" Foreground="{DynamicResource TextColor}" HorizontalContentAlignment="Stretch" Style="{DynamicResource musicListBox}">
            <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
                            <EventSetter Event="MouseDoubleClick" Handler="ListBoxItem_MouseDoubleClick"/>
                            <Setter Property="Template">
                                    <Setter.Value>
                                            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                                    <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                                                            <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                                                    </Border>
                                                   <ControlTemplate.Triggers>
                                                           <MultiTrigger>
                                                                   <MultiTrigger.Conditions>
                                                                           <Condition Property="IsMouseOver" Value="true"/>
                                                                   </MultiTrigger.Conditions>
                                                                   <Setter Property="Background" TargetName="Bd" Value="{DynamicResource LigthGray}"/>
                                                                   <Setter Property="BorderBrush" TargetName="Bd" Value="{DynamicResource LigthGray}"/>
                                                           </MultiTrigger>
                                                           <MultiTrigger>
                                                                   <MultiTrigger.Conditions>
                                                                           <Condition Property="Selector.IsSelectionActive" Value="False"/>
                                                                           <Condition Property="IsSelected" Value="true"/>
                                                                   </MultiTrigger.Conditions>
                                                                   <Setter Property="BorderBrush" TargetName="Bd" Value="{x:Null}"/>
                                                                   <Setter Property="Background" Value="{DynamicResource LigthGray}"/>
                                                           </MultiTrigger>
                                                           <MultiTrigger>
                                                                   <MultiTrigger.Conditions>
                                                                           <Condition Property="Selector.IsSelectionActive" Value="True"/>
                                                                           <Condition Property="IsSelected" Value="true"/>
                                                                   </MultiTrigger.Conditions>
                                                                   <Setter Property="BorderBrush" TargetName="Bd" Value="{x:Null}"/>
                                                                   <Setter Property="Background" Value="{DynamicResource LigthGray}"/>
                                                           </MultiTrigger>
                                                           <Trigger Property="IsEnabled" Value="False">
                                                                   <Setter Property="TextElement.Foreground" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                                                           </Trigger>
                                                   </ControlTemplate.Triggers>
                                           </ControlTemplate>
                                   </Setter.Value>
                           </Setter>
                   </Style>
           </ListBox.ItemContainerStyle>
           <ListBox.ItemTemplate>
                   <DataTemplate>
                           <Grid>
                                   <Grid.ColumnDefinitions>
                                           <ColumnDefinition Width="35*"/>
                                           <ColumnDefinition Width="25*"/>
                                           <ColumnDefinition Width="30*"/>
                                           <ColumnDefinition Width="60"/>
                                   </Grid.ColumnDefinitions>

                                   <TextBlock Text="{Binding Name}" Padding="4,0,4,0" Grid.Column="0"/>
                                   <TextBlock Text="{Binding Artist}" Padding="4,0,4,0" Grid.Column="1"/>
                                   <TextBlock Text="{Binding Album}" Padding="4,0,4,0" Grid.Column="2"/>
                                   <TextBlock Text="{Binding Year}" Padding="4,0,4,0" Grid.Column="3"/>
                           </Grid>
                   </DataTemplate>
           </ListBox.ItemTemplate>
   </ListBox>

此列表框用于显示音乐库。我也有相同的队列列表框。

当歌曲在两个列表框中播放时,我想更改 ListBoxItem 的前景。有什么办法吗?

我不想使用 SelectedItem,因为我希望播放歌曲的颜色与 SelectedItem 不同。

假设数据模型有 属性 IsPlaying,您可以使用以下 DataTrigger 来切换项目容器的前景:

<ListView>
  <ListView.ItemContainerStyle>
    <Style TargetType="ListBoxItem">
      <Setter Property="Foreground" Value="Black" />

      <Style.Triggers>
        <DataTrigger Binding="{Binding IsPlaying}" Value="True">
          <Setter Property="Foreground" Value="Red" />
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </ListView.ItemContainerStyle>
</ListView>