Jamaxack
Jamaxack

Reputation: 2460

WPF: ListBoxItem background not chenging

I have a question about ListBoxItem styling and trigger. I created one test project. In the List i have items. and i created style and trigger for this ListBox. When i hover to item IsMouseOver trigger works and chenges Margin,FontSize,Cursor,Foreground but not chenging Background and TesxDecoretions to underline. Here is test projects code.

XAML

<Window x:Class="TestForJamshed.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <Style TargetType="ListBox" x:Key="PanelPreviewShortListBox">
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Grid Background="{TemplateBinding Background}">
                        <ItemsPresenter Margin="-10 0 0 0"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="ItemContainerStyle">
            <Setter.Value>
                <Style TargetType="ListBoxItem">
                    <Setter Property="Margin" Value="10 0 0 0"/>
                    <Setter Property="BorderThickness" Value="0"/>

                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Margin" Value="-10 0 0 0"/>
                            <Setter Property="TextBlock.FontSize" Value="15"/>
                            <Setter Property="Cursor" Value="Hand"/>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Margin" Value="-10 0 0 0"/>
                            <Setter Property="Background" Value="Red"/>
                            <Setter Property="TextBlock.FontSize" Value="15"/> 
                            <Setter Property="Cursor" Value="Hand"/>
                            <Setter Property="Foreground" Value="#0000FF"/>
                            <Setter Property="TextBlock.TextDecorations" Value="Underline"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
        <ListBox Name="uiListBox" HorizontalAlignment="Right" Style="{DynamicResource PanelPreviewShortListBox}" Width="200" Background="LightGreen"/>
    </StackPanel>
</Grid>

CODE

 List<string> list = new List<string>();
        list.Add("This is an item.");
        list.Add("This is an item.");
        list.Add("This is an item.");
        list.Add("This is an item.");
        list.Add("This is an item.");
        list.Add("This is an item.");
        list.Add("This is an item.");

        uiListBox.ItemsSource = list;

Thanx!

Upvotes: 0

Views: 80

Answers (1)

Jamaxack
Jamaxack

Reputation: 2460

TargetType was ListBox and I changed it to ListBoxItem and it warks!

Upvotes: 1

Related Questions