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屋!
查看全文