CoderForHire
CoderForHire

Reputation: 439

WPF ListBoxItem Style Not Working

I have a Border inside a DataTemplate for a ListBox. When the mouse is over the item, I want to show the Border. Here's my XAML:

<ListBox Grid.Row="3"
            Grid.Column="0"
            ItemsSource="{Binding Devices}"
            SelectedItem="{Binding SelectedDevice}"
            MaxWidth="350">

    <ListBox.Resources>
        <Style TargetType="ListBoxItem">
            <Style.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/>
            </Style.Resources>
        </Style>
    </ListBox.Resources>

    <ListBox.ItemTemplate>
        <DataTemplate>

            <Border BorderBrush="SteelBlue"
                    BorderThickness="0"
                    HorizontalAlignment="Stretch"
                    CornerRadius="3"
                    MinHeight="65"
                    Margin="3">

                <Border.Style>
                    <Style>
                        <Style.Triggers>
                            <Trigger Property="Border.IsMouseOver" Value="True">
                                <Setter Property="Border.BorderThickness" Value="1" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </Border.Style>

                <Grid>

                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>

                    <Image Grid.Row="0" 
                            Grid.RowSpan="2"
                            Grid.Column="0"
                            Source="/DFT.Falcon6.UI.Desktop;component/Media/Images/fc6logo.png"
                            Height="50"
                            Width="50"
                            Margin="5"/>
                    <TextBlock Grid.Row="0"
                                Grid.Column="1"
                                Text="{Binding UnitIdentifier}"
                                Style="{StaticResource devicetextStyle}"
                                Margin="2"/>
                    <TextBlock Grid.Row="1"
                                Grid.Column="1"
                                Text="{Binding IPAddress}"
                                Style="{StaticResource devicetextStyle}"
                                Margin="2"/>

                </Grid>

            </Border>

        </DataTemplate>
    </ListBox.ItemTemplate>

</ListBox>

You can see that I have a trigger defined in the Border control to set the BorderThickness to 1. However when I run it and mouse over the item, nothing happens.

What am I doing wrong here?

Thanks

Upvotes: 0

Views: 278

Answers (1)

Bradley Uffner
Bradley Uffner

Reputation: 16991

Try this:

<Style>
    <Setter Property="BorderThickness" Value="0" />
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="BorderThickness" Value="1" />
         </Trigger>
    </Style.Triggers>
</Style>

The style is applied to the Border control, so there is no need to specify "Border" again on the bindings. I've found that it sometimes (always?) seems that an attribute defined directly on the control will override the change from a trigger. You will also have to remove the borderTickness attribute from the Border control as well.

Also, keep an eye on the Output Window while running. Binding errors will happen silently in the application, but generally show up as red text in the output window that can sometimes help track down errors.

Upvotes: 2

Related Questions