WPF ListBoxItem在自定义项中检测IsMouseOver

时间:2017-08-03 14:47:45

标签: c# wpf listbox listboxitem ismouseover

我有一个ListBox,我填写自定义项目。我想从项目中的ListBoxItem中检测MouseOver事件,以便更改按钮的可见性。我已经检查了StackOverflow上的大多数答案,following解决方案是我正在寻找的,但它不起作用。

这是我的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>

我一直在尝试以这种方式检测事件(来自我添加到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>

非常感谢任何帮助。

编辑: 我找到了一个解决方案,问题是我使用了

<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.Triggers>
                                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem},Path=IsMouseOver}" Value="True">
                                    <Setter Property="Visibility" Value="Visible" />
                                </DataTrigger>
                            </Style.Triggers>

这解决了这个问题。

1 个答案:

答案 0 :(得分:0)

我一直在寻找同样的东西。虽然问题中提供了答案,但为了更清楚地指定答案,以下是对我有用的代码。

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