WPF ListBoxItem 在自定义项中检测 IsMouseOver
Posted
技术标签:
【中文标题】WPF ListBoxItem 在自定义项中检测 IsMouseOver【英文标题】:WPF ListBoxItem detect IsMouseOver inside a custom item 【发布时间】:2018-01-11 06:14:30 【问题描述】:我有一个列表框,我用自定义项目填充。我想从项目内的 ListBoxItem 检测 MouseOver 事件,以更改按钮的可见性。我已经检查了 *** 上的大部分答案,following 解决方案正是我想要的,但它不起作用。
这是来自我的 ContactsView 的代码 sn-p:
<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>
任何帮助将不胜感激。
【问题讨论】:
请不要在问题中编辑解决方案公告。接受(即单击旁边的“勾选”)现有答案之一,如果有的话。如果现有答案尚未涵盖您的解决方案,您还可以创建自己的答案,甚至接受它。 【参考方案1】:我一直在寻找同样的东西。虽然问题中提供了答案,但为了更清楚地指定答案,以下是适合我的代码。
<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>
【讨论】:
以上是关于WPF ListBoxItem 在自定义项中检测 IsMouseOver的主要内容,如果未能解决你的问题,请参考以下文章