Wpf 使用触发器更改ListBoxItem.前景色

Wpf 使用触发器更改ListBoxItem.前景色,wpf,xaml,Wpf,Xaml,我有这个列表框样式,但由于某些原因,我无法根据触发器更改前景色。当列表框中的项目被选中时,我想将前景颜色更改为黑色。我到处搜索,所有的解决方案在我的案例中似乎都不起作用。这是我的列表框 <ListBox IsSynchronizedWithCurrentItem="True" SelectedValuePath="RecordId" ItemsSource="{Binding People}" SelectedValue="{Binding SelectedPersonId}" Horiz

我有这个列表框样式,但由于某些原因,我无法根据触发器更改前景色。当列表框中的项目被选中时,我想将前景颜色更改为黑色。我到处搜索,所有的解决方案在我的案例中似乎都不起作用。这是我的列表框

<ListBox IsSynchronizedWithCurrentItem="True" SelectedValuePath="RecordId" ItemsSource="{Binding People}" SelectedValue="{Binding SelectedPersonId}" HorizontalAlignment="Stretch" Margin="20,5,10,5" Width="Auto" Grid.Row="1" Background="{x:Null}" BorderBrush="{x:Null}" x:Name="lstCustomers">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid Height="Auto" Width="Auto" d:DesignWidth="545.375" d:DesignHeight="50.294">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="132.375"/>
                </Grid.ColumnDefinitions>
                <StackPanel x:Name="stackPanel" Orientation="Horizontal" HorizontalAlignment="Stretch">
                    <Label FontSize="16" Content="{Binding FullName}" Foreground="White"/>
                    <Label FontSize="16" Content="{Binding DisplayName}" Margin="5,0,0,0" Foreground="White"/>
                </StackPanel>
                <DockPanel LastChildFill="True" HorizontalAlignment="Stretch" Margin="0" Grid.Column="1">
                    <Label Content="{Binding Date}" ContentStringFormat="yyyy/MM/dd" FontSize="24" HorizontalAlignment="Right" Foreground="White" DockPanel.Dock="Right" VerticalAlignment="Center"/>
                </DockPanel>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="FocusVisualStyle" Value="{x:Null}" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Border BorderBrush="#26FFFFFF" BorderThickness="3" CornerRadius="5" Name="Border" Margin="0,0,2,3" Padding="0" SnapsToDevicePixels="true">
                            <ContentPresenter />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter TargetName="Border" Property="Background" Value="#7FFFFFFF" />
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter TargetName="Border" Property="Background" Value="#0FFFFFFF"></Setter>
                                <Setter TargetName="Border" Property="BorderBrush" Value="#BEFFFFFF"></Setter>
                            </Trigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="Selector.IsSelected" Value="true"></Condition>
                                    <Condition Property="IsMouseOver" Value="true"></Condition>
                                </MultiTrigger.Conditions>
                                <Setter TargetName="Border" Property="Background" Value="#7FFFFFFF"/>
                            </MultiTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

任何帮助都将不胜感激


谢谢大家!

您的触发器工作正常,但是我没有看到任何指定在选择某个项目时前景应为黑色的内容

  <ControlTemplate TargetType="ListBoxItem">
        <Border BorderBrush="#26FFFFFF" BorderThickness="3" CornerRadius="5" Name="Border" Margin="0,0,2,3" Padding="0" SnapsToDevicePixels="true">
            <ContentPresenter />
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsSelected" Value="true">
                <Setter TargetName="Border" Property="Background" Value="#7FFFFFFF" />
                <Setter TargetName="Border" Property="Foreground" Value="Black" />
            </Trigger>
            <Trigger Property="IsMouseOver" Value="true">
                <Setter TargetName="Border" Property="Background" Value="#0FFFFFFF"></Setter>
                <Setter TargetName="Border" Property="BorderBrush" Value="#BEFFFFFF"></Setter>
            </Trigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="Selector.IsSelected" Value="true"></Condition>
                    <Condition Property="IsMouseOver" Value="true"></Condition>
                </MultiTrigger.Conditions>
                <Setter TargetName="Border" Property="Background" Value="#7FFFFFFF"/>
                <Setter TargetName="Border" Property="Foreground" Value="Black" />
            </MultiTrigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>




前两个字符用于Alpha频道。@Lukasz是的,WPF不会这样读取Alpha频道。摆脱它们,它会很好地工作。如果要设置透明度,可以使用
Opacity
属性从颜色值中删除Alpha通道会破坏我的样式。如果在触发器上选择了IsSelected,我移除了7F(127),则所选项目现在完全为白色。我试过了,但没用。你能给我举个例子说明你将如何修复它吗?另外,如果WPF不能像这样工作,为什么混合设置所有的阿尔法通道的颜色?谢谢@卢卡斯哦,你说得对,我在看别的东西。无论如何,您的触发器是好的,但问题是您在ListBoxItem内的标签上设置了前景颜色,这将覆盖
ListBoxItem.foreground
颜色。我更新了我的答案。我想感谢你在这方面的帮助。我们现在在同一点上。我已经试着去做你刚才给我看的,但是你不能在边界控制上设置前景。我试着将样式设置为,但效果不太好,所以我被卡住了。我可以更改为TextBlock,并让白色从父项继承,但这仍然无法帮助我在IsSelected上设置为黑色…是否要在选择列表框时将标签的前景色更改为黑色?是,这是正确的。问题是我无法在边框控件上设置前景,因此我不知道如何在引用数据模板中实际项的模板中设置样式。。。
<Style TargetType="{x:Type ListBoxItem}">
    <Setter Property="Foreground" Value="White" />
    <Setter Property="FocusVisualStyle" Value="{x:Null}" />
    <Setter Property="Template">
        ...
    </Setter>
</Style>
<Label Text="Name" Foreground="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, Path=Foreground}"/>
<TextBlock Text="Name" />