XAML停止列表框文本样式在散焦时更改

时间:2011-09-29 13:43:54

标签: c# xaml listbox styles

我有一个包含ListBox的.NET 4 XAML程序的问题。 当列表框失去焦点时,文本变为灰色而不是设置的白色。背景确实这样做但我用

解决了这个问题
    <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#376807" />
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#487918" />
    </Style.Resources>

我尝试了几种解决方法,包括

    <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Foreground" Value="#FFFFFF" />
            </Trigger>
            <Trigger Property="IsSelected" Value="False">
                <Setter Property="Foreground" Value="#444444" />
            </Trigger>
     </Style.Triggers>

但没有成功......

enter image description here

1 个答案:

答案 0 :(得分:2)

确定我已编辑了我的代码。只要检查一下是否满足您的需求,如果没有,请返回。

以下内容已尝试使用列表框中的一些虚拟数据并且可以正常工作。我希望这就是你所寻找的,如果没有,那么请澄清一些。

                              

    <DataTemplate x:Key="SelectedTemplate">
        <TextBlock Text="{Binding}" Background="Green" Foreground="White" />
    </DataTemplate>

    <Style TargetType="{x:Type ListBoxItem}" x:Key="ContainerStyle">
        <Setter Property="ContentTemplate" Value="{StaticResource ItemTemplate}" />
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="Green" />
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<StackPanel>
    <ListBox x:Name="dummyList" ItemContainerStyle="{StaticResource ContainerStyle}" HorizontalContentAlignment="Stretch" >
    <ListBoxItem Content="A" />
    <ListBoxItem Content="B" />
    <ListBoxItem Content="C" />
</ListBox>
<Button Height="31" Width="61" Content="Click"/>
    <ListBox Name="listBox2" Height="100" Width="120" HorizontalContentAlignment="Stretch">
    <ListBoxItem Content="XX" />
        <ListBoxItem Content="YY" />
    </ListBox>
</StackPanel>