WPF ListBoxItem detect IsMouseOver inside a custom item

514 Views Asked by At

i have a ListBox which I fill with custom items. I want to detect a MouseOver event from a ListBoxItem inside the item in order to change visibility of a button. I have checked most of the answers on StackOverflow, the following solution was what I was looking for, but it doesn't work.

This is a code snippet from my ContactsView:

<ListBox ScrollViewer.CanContentScroll="False" VerticalContentAlignment="Top" ScrollViewer.ScrollChanged="ListBox_OnScrollChanged"  BorderThickness="0,0,0,0" Margin="0,0,0,0" Padding="0" BorderBrush="{StaticResource ResourceKey=PrimaryColor}" Name="ListBox" ItemsSource="{Binding ListBoxItemsSource}" HorizontalContentAlignment="Stretch">
        <i:Interaction.Triggers>
            <events:RoutedEventTrigger RoutedEvent="ScrollViewer.ScrollChanged">
                <i:InvokeCommandAction Command="{Binding Path=ListBoxScrollChangedCommand}" />
            </events:RoutedEventTrigger>
            <i:EventTrigger EventName="Loaded">
                <i:InvokeCommandAction Command="{Binding Path=ListBoxLoadedCommand}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>

        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="Padding" Value="0"/>
                <Setter Property="BorderThickness" Value="0"/>

                <Style.Triggers>
                    <Trigger Property="ListBoxItem.IsMouseOver" Value="True">
                        <Setter Property="Background" Value="{StaticResource PrimaryColor}"/>
                    </Trigger>
                    <Trigger Property="ListBoxItem.IsMouseOver" Value="False">
                        <Setter Property="Background" Value="Transparent"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
            
        </ListBox.ItemContainerStyle>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <!-- Custom item -->
                    <items:ItemCorporateContact Value="{Binding Path=., Mode=TwoWay}" HorizontalAlignment="Stretch" VerticalAlignment="Top" />
                    <Separator Height="1" Margin="0" Background="#ececec" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

And I have been trying to detect the event this way (code from a custom item that I add to a ListBox):

<Button Name="StartCallButton" VerticalAlignment="Center" Background="Red" Margin="10" HorizontalAlignment="Left">
                    <Button.Content>
                        <Image Source="{StaticResource PhoneIconBitmap}"></Image>
                    </Button.Content>
                    <Button.Style>
                        <Style TargetType="{x:Type Button}">
                            <Setter Property="Visibility" Value="Hidden" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}},Path=IsMouseOver}" Value="True">
                                    <Setter Property="Visibility" Value="Visible" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Button.Style>
                </Button>

Any help will be greatly appreciated.

1

There are 1 best solutions below

0
On

I had been searching for the same thing. Although the answer is provided in the question, but to specify the answer more clearly, following is the code that works for me.

                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel x:Name="ViewTypeStackPanel" Orientation="Horizontal">
                            <Border BorderThickness="2,0,0,0" Visibility="{Binding Path=IsSelected, Converter={StaticResource BooleanToVisibilityConverterInstance}}" BorderBrush="Blue"/>
                            <Image Height="32" Width="32">
                                <Image.Style>
                                    <Style TargetType="Image">
                                        <Setter Property="Source" Value="{Binding Path=ImagePath}"/>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsMouseOver}" Value="True">
                                                <Setter Property="Source" Value="{Binding Path=ImagePathHover}"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </Image.Style>
                            </Image>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>