WPF 列表框在 MouseOver 上的 ItemTemplate 中显示按钮 [英] WPF Listbox Show Button in ItemTemplate on MouseOver
本文介绍了WPF 列表框在 MouseOver 上的 ItemTemplate 中显示按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含图像和按钮的列表框.默认情况下,该按钮是隐藏的.每当我将鼠标悬停在列表框中的项目上时,我都想让按钮可见.我正在使用的 XAML 如下.谢谢
I have a listbox containing and image and a button. By default the button is hidden. I want to make the button visible whenever I hover over an item in the listbox. The XAML I am using is below. Thanks
<Window.Resources>
<Style TargetType="{x:Type ListBox}">
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<Border BorderBrush="Black" BorderThickness="1" Margin="6">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Path=FullPath}" Height="150" Width="150"/>
<Button x:Name="sideButton" Width="20" Visibility="Hidden"/>
</StackPanel>
</Border>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
推荐答案
好的,在你的按钮声明中试试这个:
Ok, try this in your button declaration:
<Button x:Name="sideButton" Width="20">
<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>
所以我使用带有触发器的样式来回顾可视化树,直到找到 ListBoxItem
,并且当它的 IsMouseOver
属性翻转到 True
我将 button
的可见性设置为 Visible
.
So I'm using a style with a trigger to look back up the visual tree until I find a ListBoxItem
, and when its IsMouseOver
property flips over to True
I set the button
's visibility to Visible
.
看看它是否接近你想要的.
See if it's close to what you want.
这篇关于WPF 列表框在 MouseOver 上的 ItemTemplate 中显示按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文