C# WPF SystemColors.HighlightTextBrushKey isfreezed=True;如何更改ListBox控件上所选项目的前景

C# WPF SystemColors.HighlightTextBrushKey isfreezed=True;如何更改ListBox控件上所选项目的前景,c#,.net,wpf,wpf-controls,C#,.net,Wpf,Wpf Controls,我有这个Xaml <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Value="#123456"/> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Value="White"/> 第一个有效,第二个无效。在MSDN上,SystemColors.Highl

我有这个Xaml

<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
    Value="#123456"/>        
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" 
    Value="White"/>

第一个有效,第二个无效。在MSDN上,SystemColors.HighlightTextBrushKey表示它“已冻结”且无法更改,因此我以自己的方式尝试了此功能:

<Trigger Property="IsSelected" Value="true">
    <Setter Property="Foreground" Value="White" />
</Trigger>

这也不起作用,任何指导都会很有帮助,谢谢

编辑——附加Xaml

    <ListBox ItemContainerStyle="{DynamicResource ListBoxItemStyle}" 
        AlternationCount="2"
        Margin="8,37,8,74" 
        x:Name="listClientOUContents" 
        HorizontalContentAlignment="Stretch">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Height="22" HorizontalAlignment="Stretch">
                    <Image Margin="-2,0,0,0" Source="{Binding Path=ADsPath, Converter={StaticResource ImxConverter}}" HorizontalAlignment="Left" Width="22"  />
                    <TextBlock HorizontalAlignment="Stretch" Margin="20,3,0,0" Text="{Binding Path=DisplayValue}" />
                    <Rectangle HorizontalAlignment="Stretch" Fill="White" Stroke="White" Margin="-2,0,-2,0.5" VerticalAlignment="Bottom" Height="1" />
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

这是我正在使用的ItemContainerStyle

<Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
        <Setter Property="FontSize" Value="12" />
        <Setter Property="FontFamily" Value="Tahoma" />
        <Setter Property="Background" Value="#006C3B3B"/>
        <Style.Resources>
            <!-- Selected and Focused -->
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FF533F70"/>
            <!-- Selected and UN-focused -->  
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#FF533F70"/>
            <Storyboard x:Key="MouseOverStoryBoard">
                <ColorAnimationUsingKeyFrames AutoReverse="True" BeginTime="00:00:00" Storyboard.TargetName="{x:Null}" Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
                    <SplineColorKeyFrame KeyTime="00:00:00" Value="#FFF48F21"/>
                    <SplineColorKeyFrame KeyTime="00:00:00.5000000" Value="#FF4A475C"/>
                </ColorAnimationUsingKeyFrames>
            </Storyboard>
        </Style.Resources>
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="true">
                <Setter Property="Foreground" Value="White" />
            </Trigger>
            <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                <Setter Property="Background">
                    <Setter.Value>
                        <SolidColorBrush Color="#a1a1a1"/>
                    </Setter.Value>
                </Setter>
                <Setter Property="Foreground" Value="black"/>
            </Trigger>
            <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                <Setter Property="Background" Value="#a1a1a1"/>
                <Setter Property="Foreground" Value="black"/>
            </Trigger>
            <Trigger Property="IsMouseOver" Value="True">
                <Trigger.EnterActions>
                    <BeginStoryboard Storyboard="{StaticResource MouseOverStoryBoard}"/>
                </Trigger.EnterActions>
                <Setter Property="Foreground" Value="White" />
                <Setter Property="Background" Value="#FFF48F21"/>
                <Setter Property="FontStyle" Value="Normal"/>
            </Trigger>
        </Style.Triggers>
    </Style>

编辑:切换“IsSelected”触发器和“ItemsControl.AlternationIndex”触发器的顺序:

<Trigger Property="ItemsControl.AlternationIndex" Value="0">
    <Setter Property="Background">
        <Setter.Value>
            <SolidColorBrush Color="#a1a1a1"/>
        </Setter.Value>
    </Setter>
    <Setter Property="Foreground" Value="black"/>
</Trigger>
<Trigger Property="IsSelected" Value="true">
    <Setter Property="Foreground" Value="White" />
</Trigger>


列表中的最后一个触发器具有优先级,因此您的AlternationIndex覆盖了您的IsSelected触发器。IsSelected列在AlternationIndex之后,它现在应该具有优先级。

编辑:切换“IsSelected”触发器和“ItemsControl.AlternationIndex”触发器的顺序:

<Trigger Property="ItemsControl.AlternationIndex" Value="0">
    <Setter Property="Background">
        <Setter.Value>
            <SolidColorBrush Color="#a1a1a1"/>
        </Setter.Value>
    </Setter>
    <Setter Property="Foreground" Value="black"/>
</Trigger>
<Trigger Property="IsSelected" Value="true">
    <Setter Property="Foreground" Value="White" />
</Trigger>


列表中的最后一个触发器具有优先级,因此您的AlternationIndex覆盖了您的IsSelected触发器。IsSelected列在AlternationIndex之后,它现在应该具有优先权。

能否显示更多XAML(列表框元素和列表框中包含的所有相关元素)?是。。。我用更多的xaml编辑了这个问题。你能显示更多的xaml(列表框元素和列表框中包含的所有相关元素)吗?是的。。。我用更多的xaml编辑了这个问题。