开发者

XAML Stop List Box Text Style Changing on Defocus

I have an issue with a .NET 4 XAML program which con开发者_运维问答tains a ListBox. When a the list box looses focus the text turns to grey rather than the set white colour. The background did do this but I resolved that with

    <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#376807" />
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#487918" />
    </Style.Resources>

I have tried several methods of resolving this including

    <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>

But not have been successful...

XAML Stop List Box Text Style Changing on Defocus


OK I have edited my code. Just check if this satisfies your need, if not then please revert.

The following has been tried with some dummy data in the listbox and it works. I hope this is what you were looking for, if not, then please clarify some more.

    <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> 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜